In C, when you used the name of an array in an expression (including passing it to a function), unless it is the operand of the address-of (&
) operator or the sizeof
operator, it decays to a pointer to its first element.
That is, in most contexts array
is equivalent to &array[0]
in both type and value.
In your example, my_array
has type char[100]
which decays to a char*
when you pass it to printf.
&my_array
has type char (*)[100]
(pointer to array of 100 char
). As it is the operand to &
, this is one of the cases that my_array
doesn't immediately decay to a pointer to its first element.
The pointer to the array has the same address value as a pointer to the first element of the array as an array object is just a contiguous sequence of its elements, but a pointer to an array has a different type to a pointer to an element of that array. This is important when you do pointer arithmetic on the two types of pointer.
pointer_to_array
has type char *
- initialized to point at the first element of the array as that is what my_array
decays to in the initializer expression - and &pointer_to_array
has type char **
(pointer to a pointer to a char
).
Of these: my_array
(after decay to char*
), &my_array
and pointer_to_array
all point directly at either the array or the first element of the array and so have the same address value.