The following code...
int array[] = {17, 18, 19};
printf("Location of array: %p\n", array);
printf(" Value of array: %d\n", *array);
printf(" Size of array: %d bytes\n", sizeof(array));
Produces the output
Location of array: 0x7ffd0491c574
Value of array: 17
Size of array: 12 bytes
When I use variable array on the second line, it refers to the location of the "17". When I use it on the third like, it dereferences the pointer and prints out the number 17. Those, I understand.
On the last line, it prints out "12 bytes" as the size of the array. Why doesn't it print out 4 bytes, since in the previous two uses of the same variable, it seems to exclusively refer to the zero index of the array? How does sizeof
know to look at the remainder of the array, instead of just printing out 4 bytes (as it would if I ran (sizeof(*array)
)?