2

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

James
  • 787
  • 1
  • 7
  • 15
  • `array` is the whole array, `*array` (aka. `array[0]`), is the first element – M.M Dec 11 '17 at 05:32
  • 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)` – ikegami Dec 11 '17 at 05:41

6 Answers6

5

When passed to sizeof, an array name doesn't decay into pointer, so the whole array size is found.

In the first case, it decays into pointer to the first 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 the C standard $6.5.3.4 (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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
user2736738
  • 30,591
  • 5
  • 42
  • 56
  • Can you please provide the link you got this information from? – surendra nath Dec 11 '17 at 06:39
  • @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. – user2736738 Dec 11 '17 at 06:54
1

Here,

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

sizeof int generally 4 bytes. sizeof gives 12 bytes.

msc
  • 33,420
  • 29
  • 119
  • 214
  • "*`sizeof(3 * 4)`*": well, well ... - this would evaluate to the size of exactly *one* `int`. – alk Dec 31 '17 at 12:38
1

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.

alk
  • 69,737
  • 10
  • 105
  • 255
Ganeshdip
  • 389
  • 2
  • 10
0

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

<int0><int1><int2>
^
array

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.

Déjà vu
  • 28,223
  • 6
  • 72
  • 100
0

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

NishanthSpShetty
  • 661
  • 5
  • 14
-1

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

Amin Negm-Awad
  • 16,582
  • 3
  • 35
  • 50