sizeof a
returns the size of the entire array in storage units (bytes). sizeof (int)
returns the size of a single integer in storage units. Since a
is an array of 3 integers, sizeof a / sizeof (int)
gives you 3.
They always taught me that an array that it is a pointer
"They" taught you incorrectly. Unless it is the operand of the sizeof
or unary &
operators, an array expression will be converted ("decay") to a pointer expression, and the value of the pointer expression will be the address of the first element of the array, but an array object is not a pointer.
When you declare a
, what you get in memory looks something like this:
+---+
a:| | a[0]
+---+
| | a[1]
+---+
| | a[2]
+---+
There is no object a
that's separate from the array elements; there's no pointer anywhere.