Example code
int main() {
char *s = "kut";
char **p = &s;
printf("*s: %c\n", *s);
printf("s: %p\n", s);
printf("&s[0]: %p\n", &s[0]);
printf("p: %p\n", p);
printf("*p: %p\n", *p);
printf("**p: %c\n", **p);
printf("p[0]: %s\n", p[0]);
printf("&p[0]: %p\n", &p[0]);
return 0;
}
Output:
*s: k
s: 0x1043acf46
&s[0]: 0x1043acf46
p: 0x7ffeeb853670
*p: 0x1043acf46
**p: k
p[0]: kut
&p[0]: 0x7ffeeb853670
How does it come that &p[0]
(address of p[0]
) does print out the string kut
? The output show that the address of p[0]
is the same as p
(as p
is just a constant pointer pointing to the first element of the array).
For any pointer or array p
and index i
, the expression p[i]
is exactly equal to *(p + i)
.
If i == 0
then we have p[0]
which is equal to *(p + 0)
which in turn is equal to *(p)
which is the same as *p
.
And in your case *p
is the same as s
.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments