Except when it is the operand of the sizeof
or unary &
operators, or is a string literal used to initialize a character array in a declaration, an expression of type "N-element array of T
" will be converted, or "decay", to an expression of type "pointer to T
" and the value of the expression is the address of the first element of the array.
This is true of all array types, not just character arrays.
So when you write
scanf("%9s", name);
the expression name
is converted from type "10-element array of char
" to "pointer to char
", and what scanf
receives is the address of the first element of the array.
This is important - arrays are not pointers; array expressions are converted to pointers as necessary.
Believe it or not, there is a reason for this behavior. C was derived from an earlier language called B. In B, array objects had an explicit pointer to the first element, and the subscript operation a[i]
was defined as *(a + i)
- given a starting address a
, offset i
elements from that address and dereference the result.
When designing C, Ritchie wanted to keep B’s array semantics, but he didn’t want to keep the explicit pointer to the first element that those semantics required. So instead of creating storage for the pointer, he created the rule that the array expression is converted to a pointer expression when necessary.
And this is why arrays are weird.