The question is: Which size has the array int_array[]?int int_array [] = { 5, 10, 13, 24};
Remember:int array_size = sizeof(int_array)/sizeof(int_array[0]); int max_nr_in_array = array_size-1;
But:
If you give the array as an argument to a function, inside the function
you can´t compute the arraysize!
Example:
Here you can look for an example programme
or the same as html-output.
You can download the example (incl. html-output) and compile it with:
Start it with:cc -Wall -O2 -o array1 array1.c
The output you can see here:./array1
The sizeof(int) is: 4 The sizeof(array) is: 12 The sizeof(array[0]) is: 4 The sizeof(*array) is: 4The address of array is: 0xbffff87c The address of &array[0] is: 0xbffff87c The address of &array[1] is: 0xbffff880 The address of array+1 is: 0xbffff880The value of *array is: 1 The value of array[0] is: 1 The value of *array+1 is: 2 The value of array[1] is: 2The number of elements in array is: 3 The number of elements in array is: 3The address of array in prtfunc is: 0xbffff87c The sizeof(array) in prtfunc is: 4 The sizeof(&array) in prtfunc is: 4array[0] = 1 and has address: 0xbffff87c array[1] = 2 and has address: 0xbffff880 array[2] = 3 and has address: 0xbffff884The following thinks only work with a pointer to our array - because array is a const pointer to first element and can´t change [get an compiler error]! Get this pointer with: int *a_p = array;To increment the second element do: ++(*(++a_p)) New values are: array[0] = 1 and has address: 0xbffff87c array[1] = 3 and has address: 0xbffff880 array[2] = 3 and has address: 0xbffff884To increment the first element do: ++(*a_p) New values are: array[0] = 2 and has address: 0xbffff87c array[1] = 3 and has address: 0xbffff880 array[2] = 3 and has address: 0xbffff884
Compute the size with descripe before and use the array.const char *days[] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
Advantage of this way:
If you append or insert or delete an element you don´t must change
your source!