str
is typed char[100]
.
So taking it's address using the &
-operator, gives you an address of a char[100]
, not the address of a char
.
The relevance of the difference becomes obvious if you think on how pointer arithmetic works:
Incrementing a pointer by 1 moves its value as many bytes as the type uses the pointer points to. Which is 100 for a pointer to char[100]
and just 1 for a pointer to char
.
To define a pointer to an array the somewhat unintuitive notation
T (*<name>)[<dimension>]
is used.
So a pointer to char[100]
would be
char (*ps)[100];
So one can write:
char str[100];
char (*ps)[100] = &str; // make ps point to str
to have ps
point to the array str
.
Doing
char * p = str;
makes p
point to the 1st element of str
, which is a char
.
The interesting thing is that p
and ps
point to the same address. :-)
But if you'd do
ps = ps + 1;
ps
got incremented 100 bytes, whereas when doing
p = p + 1;
p
got incremented just one byte.