I'd like to know how to get an array rows & columns size. For instance it would be something like this:
int matrix[][] = { { 2, 3 , 4}, { 1, 5, 3 } }
The size of this one would be 2 x 3. How can I calculate this without including other libraries but stdio or stdlib?
This has some fairly limited use, but it's possible to do so with sizeof.
sizeof(matrix) = 24 // 2 * 3 ints (each int is sizeof 4)
sizeof(matrix[0]) = 12 // 3 ints
sizeof(matrix[0][0]) = 4 // 1 int
So,
int num_rows = sizeof(matrix) / sizeof(matrix[0]);
int num_cols = sizeof(matrix[0]) / sizeof(matrix[0][0]);
Or define your own macro:
#define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
int num_rows = ARRAYSIZE(matrix);
int num_cols = ARRAYSIZE(matrix[0]);
Or even:
#define NUM_ROWS(a) ARRAYSIZE(a)
int num_rows = NUM_ROWS(matrix);
#define NUM_COLS(a) ARRAYSIZE(a[0])
int num_cols = NUM_COLS(matrix);
But, be aware that you can't pass around this int[][] matrix and then use the macro trick. Unfortunately, unlike higher level languages (java, python), this extra information isn't passed around with the array (you would need a struct, or a class in c++). The matrix variable simply points to a block of memory where the matrix lives.
It cannot be "something like this". The syntax that involves two (or more) consecutive empty square brackets [][] is never valid in C language.
When you are declaring an array with an initializer, only the first size can be omitted. All remaining sizes must be explicitly present. So in your case the second size can be 3
int matrix[][3] = { { 2, 3, 4 }, { 1, 5, 3 } };
if you intended it to be 3. Or it can be 5, if you so desire
int matrix[][5] = { { 2, 3, 4 }, { 1, 5, 3 } };
In other words, you will always already "know" all sizes except the first. There's no way around it. Yet, if you want to "recall" it somewhere down the code, you can obtain it as
sizeof *matrix / sizeof **matrix
As for the first size, you can get it as
sizeof matrix / sizeof *matrix
sizeof (matrix) will give you the total size of the array, in bytes. sizeof (matrix[0]) / sizeof (matrix[0][0]) gives you the dimension of the inner array, and sizeof (matrix) / sizeof (matrix[0]) should be the outer dimension.
Example for size of :
#include <stdio.h>
#include <conio.h>
int array[6]= { 1, 2, 3, 4, 5, 6 };
void main() {
clrscr();
int len=sizeof(array)/sizeof(int);
printf("Length Of Array=%d", len);
getch();
}
Related
I wrote two functions and call the functions in main.
Function 1 – I wrote a function that returns void and takes an int * (pointer to integer array) or int[], and int (for the size). The function needs to initialize all the elements of the array to non-zero values.
Function 2 – I wrote another function that returns int and takes an const int * (pointer to integer array) or int[], and int (for the size). The function should sum all the elements of the array and return the sum.
In main I defined an integer array of size 5. Called function 1 in main to initialize the values of the array. Called function 2 in main to get the sum and print the value of the sum to the console.
My problem is the program runs but the print out for sum we are getting is a large (in the millions), random, number and is not the expected answer of 15. Anyone who can help us get the correct answer would be greatly appreciated
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#pragma warning(disable: 4996)
void func1(int* ptr, int size);
int func2(const int* ptr, int size);
int main()
{
int grid[5];
func1(grid, 5);
func2(grid, 5);
}
void func1(int* ptr, int size)
{
*ptr = 1, 2, 3, 4, 5;
}
int func2(const int* ptr, int size)
{
int sum;
sum = ptr[0] + ptr[1] + ptr[2] + ptr[3] + ptr[4]; // *(ptr + 0); putting an asterisk makes it so that it changes the entire "ptr" value and the "[0]" value
printf("\n\nThe sum of the integers in the array is %d.\n\n", &sum);
}
*ptr = 1, 2, 3, 4, 5;
does not do what you think it does. It actually evaluates all the integer constants but sets ptr[0] to be 1 (see comma operator for more detail), leaving all the others at some arbitrary value.
Note that it is not evaluating *ptr = (1, 2, 3, 4, 5) (which would set *ptr to 5) but is actually evaluating (*ptr = 1), 2, 3, 4, 5 - this works because something like 42 is actually a valid C statement, albeit not very useful.
If you're trying to set the array to increasing values, just use something like:
for (int i = 0; i < size; i++)
ptr[i] = i + 1;
You probably also want to do that when summing the values since it should depend on the passed-in size rather than just summing five values:
int sum = 0;
for (int i = 0; i < size; i++)
sum += ptr[i];
Additionally, the value you are printing out is not the sum, it's the address of the variable containing the sum (a decent compiler will warn you about this). You should be using sum in your printf rather than &sum.
And, as a final note, the signature for func2 indicates that you should actually be returning the sum rather than just printing it. So I would suggest removing the printf from that function and simply doing:
return sum;
Then you can put the printf into the caller (main) as follows:
int main(void)
{
int grid[5];
func1(grid, sizeof(grid) / sizeof(*grid));
int sum = func2(grid, sizeof(grid) / sizeof(*grid));
printf("The sum of the integers in the array is %d.\n\n", sum);
return 0;
}
Note the use of sizeof(grid) / sizeof(*grid), which is basically the number of array elements in grid - this will allow you to resize grid by simply changing it in one place to something like int grid[42] and still have all the code work with the updated size.
Not actually necessary for your code but it's best to get into good programming habits early (more descriptive names for your functions may also be a good idea).
Line *ptr = 1, 2, 3, 4, 5; assigns ptr[0] value and leaves other spots unitilized so when you sum it, it will be random memory.
You should use for like this to initialize
for(int i=0;i<size;i++)
{
ptr[i] = i+1;
}
and similiar aproach to sum it.
I am trying to understand how malloc works. I did a program searches for the largest element in a one dimensional array int.
This is the code.
#include <stdlib.h>
#include <stdio.h>
void largest_element(int *nbr)
{
int i;
int n;
int m;
i = 1;
nbr = (int*)malloc(sizeof(nbr) + 8);
while (i < 8)
{
if (*nbr < *(nbr + i))
*nbr = *(nbr + i);
i++;
}
printf("%d ", *nbr);
}
int main(void)
{
int i;
int tab[8] = {11, 2, 4, 5, 9, 7, 8, 1};
int n = sizeof(tab)/sizeof(int);
i = 0;
largest_element(&tab[8]);
return(0);
}
The program works without malloc but how can I make it work with malloc? What did I do wrong and why does my code only give me garbage numbers?
I think you are lost with pointers and arrays so you can not understand malloc properly (no offense, everyone who is learning C do the same mistake).
Let's take your main function. When you run:
int tab[8] = {11, 2, 4, 5, 9, 7, 8, 1};
You staticly allocate an array of 8 integers and you fill it with your numbers.
The dynamic equivalent would be:
int* tab = malloc(sizeof(int) * 8);
tab[0] = 11;
tab[1] = 2;
/// Etc...
tab[7] = 1;
First thing: the first element of an array has the index 0. So in your largest_element function, i should be initialized at 0 instead of 1.
The reason is, when you deal with array, you deal with pointers. In your case, tab is a pointer to the first element of the array. So, when you do tab[3], you get the forth element of your array.
Second thing: when you do:
largest_element(&tab[8]);
You send to your function the eighth element after the begining of your array. The problem is: you do not own this memory area! You own the memory only until tab[7].
If you want to send the complete array to your function, just use:
largest_element(tab);
Now, let's talk about your largest_element function.
You do not need to call malloc here since the memory is already allocated
When you do *nbr = *(nbr + i); you change the value of the first element of your array. I think you wanted to do m = *(nbr + i); isn't it.
Why do you not use the nbr[i] instead of *(nbr + i)?
A correct implementation of this function would be something like (not tested):
void largest_element(int *nbr)
{
int i = 0;
int max = 0;
while (i < 8)
{
if (max < nbr[i])
max = nbr[i];
i++;
}
printf("%d ", m);
}
A last thing, using malloc involve using the function free to release the memory when you do not need it anymore.
What did I do wrong and why does my code only give me garbage numbers??
In largest_element(int *nbr) nbr points to the array tab in main (at least if you call it like this: largest_element(tab); instead of like this largest_element(&tab[8]);
Then you call nbr = (int*)malloc(sizeof(nbr) + 8); now nbr points to some allocated memory which has not been initialized and which contains garbage values. Now if you read from that memory it's normal that you get garbage values.
You simply don't need malloc for this problem, just as you don't need floating point math or file system related functions for this problem.
it is a practice program to sort GArrays, I have used sizeof() to know the size of my arrays.
Thinking logically, sizeof(x) should be 24 i.e 6 integers * size of each integer i.e 4 - 6*4.
But when I put these integers into my GArray, the size is 8.
why is it 8, why not 32? .. as g_array_new allocates bytes in powers of 2? and nearest power of 2 near 24 is 2^5 i.e 32
/*************************************************************************************************************
* FILE NAME : ex-garray-6.c
*
* DESCRIPTION : sort Garray using GCompareFunc (Not used in GIMP, Gaim or Evolution)
*
************************************************************************************************************/
#include<glib.h>
#include<stdio.h>
/*************************************************************************************************************
* FUNCTION NAME : print_arr
*
* DESCRIPTION : prints entire array using len and g_array_index
*
* RETURNS : void
*
************************************************************************************************************/
void print_arr(GArray* arr)
{
int i = 0;
printf("\n Array : \n");
for (i = 0; i < (arr->len); i++)
{
printf("%d\n", g_array_index(arr, int, i));
}
}
/*************************************************************************************************************
* FUNCTION NAME : compare_ints
*
* DESCRIPTION : utilized qsort() to sort elements of the unsorted array.
* arguments are two gpointers.They are typecasted to int pointers
* int the function
*
* RETURNS : int - -ve if first arg is smaller than second arg
* 0 if first arg is equal to second arg
* +ve - second arg is smaller than first arg
*
************************************************************************************************************/
int compare_ints( gpointer* a, gpointer* b)
{
int* x = (int*)a;
int* y = (int*)b;
return (*x - *y);
}
/*************************************************************************************************************
* FUNCTION NAME : main.c
*
* DESCRIPTION : main.c declares GArray,allocates memory to it, appends 6 integers into the array,* uses g_array_sort to print the array, uses print_arr function to print the array * frees array at end.
*
* RETURNS : SUCCESS
*
************************************************************************************************************/
int main(int argc, char** argv)
{
// 1. declare GArray pointer variable and allocate memory to it
GArray* arr = g_array_new(FALSE, FALSE, sizeof(int));
// g_array_set_size(arr,8); - didn't work to fix size to 8 bytes
// 2. initialize int array of 6 elements say x
int x[6] = {500,400, 500, 700, 200, 300};
// 3. append in the array
arr = g_array_insert_vals(arr,0, x, 6);
printf("\n size of x : %d \n size of arr : %d", sizeof(x), sizeof(arr));
// 4. print the array
print_arr(arr);
/* 5. sort the array using
g_array_sort(
<GArray pointer variable>,
(GCompareFunc)<name of the compare function>);
- compare function uses qsort()-
-returns -ve a<b
-returns 0 a = b
-returns +ve b = a
*/
/* 5.5 alternate sorting function -
g_array_sort_with_data(
<same as g_array_sort>,
<same as g_array_sort>,
<gpointer to user-data>); */
printf("\n Array after sorting \n ");
g_array_sort(arr, (GCompareFunc)compare_ints);
// 6. print garray
print_arr(arr);
// 7. free garray
g_array_free(arr, TRUE);
}
Yes I got the answer:
GArray is a structure
having 2 elements 1 of gchar type and other of gunit type
gchar is of 1 byte
gunit is of 4 bytes
so sizeof(GArray) or sizeof(arr) here would be 1+4 = 5 then extending it to nearest power of 2 i.e 2^3 is 8 :)
As said in commentaries, arr is not an array, it is a pointer to a GArray (which is not an array neither, cf GArray reference).
x is an array, so it size is size of an item * number of items, so 6 * sizeof(int).
arr is a pointer, and pointer size depend to the compiler and system used, in your case, a int * has a size of 8.
I've made a shuffling type program that obtains from cases[]; and arranges them in a random order.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void deal(void);
int cases[22] = {1,2,3,4,5,6,8,10,15,20,25,35,50,65,85,100,105,135,165,200,250};
int main()
{
srand(time(NULL));
int a = 0;
deal();
for(a = 0 ; a < 22 ; a++)
printf("%d \n", cases[a]);
return 0;
}
void deal(void){
int loop;
int temp;
int x;
int y;
for(loop = 0; loop < 50; loop++){
x = rand() % 22;
y = rand() % 22;
temp = cases[x];
cases[x] = cases[y];
cases[y] = temp;
}
}
However when I run the code it comes with a randomly placed 0. Where is it from and why is it there?
Example output:
6
15
0 //I don't want that
20
5
50
10
3
165
65
135
105
8
1
200
25
100
85
2
250
4
You declared int cases[22] but initialized only 21 integers into that array and therefore, the last element will be 0 when not initialized. Try to add another integer at the end of the array
It's an off by one error: Ferico Samuel's answer is correct
This is a complement:
Instead of hard coding 22 as the number of cases you should write your program like this:
// no explicit declaration of the size here. The compiler automatically
// puts the correct size depending on the number of elements that follow.
int cases[] = {1,2,3,4,5,6,8,10,15,20,25,35,50,65,85,100,105,135,165,200,250};
...
for(a = 0 ; a < sizeof(cases) / sizeof(cases[0]); a++)
...
sizeof(cases) is the size of your cases array in bytes
sizeof(cases[0]) is the size of one element of the casesarray
thus sizeof(cases) / sizeof(cases[0])is the number of elements of the array.
This way you can add more elements to the cases array without the need to change the array size elsewhere in your program.
You declare your array as:
int cases[22] = {1,2,3,4,5,6,8,10,15,20,25,35,50,65,85,100,105,135,165,200,250};
Without you know it, there is actually only 21 elements in that array. Thus, in C standard compilers, the non-initialized value would be set as zero. To correct it, add one more element to the array.
int cases[22] = {1,2,3,4,5,6,8,10,15,20,25,35,50,65,85,100,105,135,165,200,250, 500}; // add 500
And things will be okay.
Anyway, for this kind of initialization, I recommend you to put the elements in multiple lines (say, every 10 elements):
int cases[22] = {1, 2, 3, 4, 5, 6, 8, 10, 15, 20,
25, 35, 50, 65, 85, 100,105,135,165,200,
250, 500}; // add 500
And put in nice format to avoid such error.
You have the wrong number of elements in the array. C is a bit stupid, because it is designed so that a compiler can only detect if there is too many elements in an array, but never if there is too few.
To ensure the exact amount of elements, you can declare arrays like this:
#define ARRAY_ITEMS(arr) (sizeof(arr) / sizeof(*arr))
#define CASES_ITEMS 22
int cases[] = {1,2,3,4,5,6,8,10,15,20,25,35,50,65,85,100,105,135,165,200,250};
_Static_assert(ARRAY_ITEMS(cases) == CASES_ITEMS, "Wrong array size: cases");
This is my first time asking so please be gentle lol.
So I am trying to better understand arrays in C. Is there a way I can add an element to an array without using a for loop? The problem is I want to add a new element to the end of the array, but without knowing the size of the array.
So I already have this:
#include <stdlib.h> //not sure if needed but put it just in case
int main(void):
float real[20];
real[]={1,2,3,4,5};
I want to add the number 6 to the array, but I don't want to use real[5]=6. Is there another way to add an element to the end of the array without a loop checking if each element in the array until the element is null? Thanks for your help in advance!
C arrays don't know about their length. If you need arrays that can grow and shrink, you have to keep extra information on how long your active array is. An array that is created on the stack like so:
real array[20] = {1, 2, 3};
will contain twenty elements, the first three initialised with concrete values, the rest initialised to zero. If you want to consider this array as an array of initially three values that can hold up to 20 values, you have to keep the actual array length as an extra variable:
real array[20] = {1, 2, 3};
int narray = 3;
You can then append a value. Take care not to overflow that maximum storage of 20 elements:
if (narray < 20) array[narray++] = 9;
You can read the last value and remove it from the array:
if (narray) printf("%g\n", array[--narry]);
Here, you have to take care not to underflow the array. (Also, don't decrement narray more than once in the same expression, which will lead to undefined behaviour.)
If you write a function that operates on the array, pass both array and length:
void array_print(float array[], int n)
{
int i;
for (i = 0; i < n; i++) {
if (i) printf(", ");
printf("%g", array[i]);
}
printf("\n");
}
and call it like so:
array_print(array, narray);
Another approach is to keep a sentinel value like 0.0. This can be useful in some cases, but it has the disadvantage that you have to traverse the whole array to find out the length. It also removes the sentinel from the range of valid values that your array can hold.
The advantage here is, of course, that the array is "self-contained", i.e. you don't have to pass array and length to a function; just the array is enough. When appending you still have to take care not to overflow the maximum storage, which makes this approach cumbersome.
The only way is to keep a pointer to the next free position in the array. For example
#include <stdio.h>
float * copy( const float *src, size_t n, float *dst )
{
while ( n-- ) *dst++ = *src++;
return dst;
}
int main(void)
{
float real[20];
float *p = real;
p = copy( ( float [] ){ 1, 2, 3, 4, 5 }, 5, p );
p = copy( ( float [] ){ 6, 7 }, 2, p );
for ( float *q = real; q != p; ++q ) printf( "%1.1f ", *q );
printf( "\n" );
return 0;
}
The output is
1.0 2.0 3.0 4.0 5.0 6.0 7.0
You always can check whether p points within the array using condition
p < real + 20
The other approach is to use a structure that contains an array. For example
struct Array
{
enum { N = 20 };
float real[N];
size_t n; /* current number of filled elements */
};
With a little help of the preprocessor, you can use a compound literal to get the size:
int main(void)
{
#define REAL_VALUES 1, 2, 3, 4, 5
float real[20] = {REAL_VALUES};
real[sizeof((float[]){REAL_VALUES}) / sizeof(float)] = 6;
return 0;
}
In C you can get the length of an array with the expression sizeof (array) / sizeof (array)[0], or even better, by defining a macro:
#define LEN(array) \
((int) (sizeof (array) / sizeof (array)[0]))
What you describe is not adding an element to the end of the array but rather adding an element after the last element inserted. For that you need a variable that keeps track of the element count:
int count = 0;
float x;
...
if (count < LEN(real)) {
real[count] = x;
count++;
}
Observe, however, that once you have passed the array to a function its length is gone, so you need to pass the array length to the function as well:
void foo(float a[], int len);
...
foo(real, LEN(real));
You can use memset() to make space for your element in your array and then insert your element at specific position.
Here is sample code below.
/*Make space for number to insert.*/
memmove(&arr[pos+1],&arr[pos],(ARR_SIZE+1-pos)*sizeof(int));
arr[pos] = num;/*insert the number.*/
Full code snipet could be found here InsertElement