when I am compiling this small program I am getting different values as output, instead of getting numbers from 0 to 5. And the size of array is always 8. The different values I am getting are:
-981774704
32767
0
0
4195728
0
Any tips would be really valuable. Thank you
#include <stdio.h>
int main() {
int array[10];
int i;
for (i = 0; i < 6; i++) {
printf("%d\n", array[i]);
}
int z = sizeof(&array);
printf("\n Size of array is %d", z);
return 0;
}
int z = sizeof(&array);returns the size of a pointer, 8 in your case. To get the size of the array, useint z = sizeof array;, which is likely 40 or 80 bytes. To get the number of elements in the array, useint z = sizeof array/sizeof array[0];which is 10. in this case.sizeofoperator to determine the size of the type and size of the object( of the result of the expression). it is not vector#size.