C int array and pointer interaction [duplicate]


This question already has an answer here:

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))?


array is the whole array, *array (aka. array[0]), is the first element
Re “[the two previous uses of array] seems to exclusively refer to the zero index of the array?“, No, only *array referred to the first index of the array, and so would sizeof(*array)



Well when passed to sizeof the array name doesn’t decay into pointer. So the whole array size is found.

In the first case it decays into pointer to the fist element and the address is printed.

In the second case we dereference the address which is basically the value at the address pointed by the array name, namely the first element.

Why doesn’t it print out 4 bytes?

From standard $ (sizeof operator)

When applied to an operand that has array type, the result is the
total number of bytes in the array.

This answers your question why when passed an array name it shows the number of bytes. Here the array has 3 elements and each of size sizeof int or 4 Bytes in your system and so the total size = 3*4 = 12

What is *array?

*array is nothing other than array[0]. array decays into pointer to the first element of the array and then we dereference it. What is the value that is there? That is the value 17.

Why sizeof(*array)=4?

Well if you remember array is an array of 3 integers. So ofcourse the value contained in it is of type int. In your system sizeof int is 4 bytes. That’s why you will get 4 as a result sizeof *array .

How sizeof works?

sizeof is implemented by the compiler. For non-VLA type of objects sizeof is a constant which is resolved compile time. But in case of VLA the array size is known at run time and that generates the result. So this is an expression for VLA.


Can you please provide the link you got this information from?
@surendranath.: Which information are you talking about? The standard draft is openly available. The quote is from there. Rest of the things have been acquired over time not within 5 minutes when the question is published. Array decaying is pretty common notion. The C standard is N1570. Check it.


Here array is the pointer to an array of 3 integers, so if you are the printing size of the array, it will give you 12 bytes as each integer takes 4 bytes.

As you know array will have starting location of the array, now you are printing integer at the address “array” by using format specifier %d. It will print integer stored in first 4 bytes of the array,i.e. 17.




sizeof(number of array elements * sizeof data_type)
sizeof(3 * 4)

sizeof int generally 4 bytes.
sizeof gives 12 bytes.



sizeof(*array) would give you sizeof(int) (seemingly 4 on your computer).

sizeof(array) gives 12 because sizeof(*array) gives 4, and you have 3 of these (int), 3 x 4.

array is an area in memory where the 3 integer are stored


sizeof(array) gives the size of that space occupied by that area.

Mentioning array as itself in, for instance, int *p = array; stores the address of array in the pointer p.



Arrays in C are not exactly pointers, which is used to be in early days. Array names are decay into pointer based on the use of identifier(variable). Array names were pointer which caused problem with structure containing the array member, storing the pointer in array eventually lead to problem with structure members. So it was decided by the author of C to make array as name to location rather than the pointer some location. When used with * it will decay into pointer. Which allow us to use the array as a[i], i[a], *(a+i) .



sizeof() is a operand evaluated at compile time. The compiler knows the size, because it creates the array.