Given char name[] = "Hello";
, if you want the address of name
, you should use &name
. That provides a pointer to an array of six char
.
The compiler should warn about char *s = &name;
because it uses a pointer to an array for a pointer to a char
, and those are different (and incompatible) types of pointers.
In C, we generally do not work with arrays. Instead, we work with pointers to array elements. To assist with this, C automatically converts an array to a pointer to its first element, except in certain situations.
Thus char *s = name;
, name
is automatically converted to a pointer to its first element, which is a char
. This is the reason we do not need to use &
with arrays, even though we need to use &
to take the address of a non-array—with an array, C automatically gives us an address, so we do not need to use &
.
Although &name
points to the start of the array and name
points (after conversion) to the first character of the array, which starts in the same place, the pointers have different types.