I was trying to figure something out with pointers, so I wrote this code:
#include <stdio.h> int main(void) { char s[] = "asd"; char **p = &s; printf("The value of s is: %p\n", s); printf("The direction of s is: %p\n", &s); printf("The value of p is: %p\n", p); printf("The direction of p is: %p\n", &p); printf("The direction of s[0] is: %p\n", &s[0]); printf("The direction of s[1] is: %p\n", &s[1]); printf("The direction of s[2] is: %p\n", &s[2]); return 0; }
When compiling with gcc, I get the following warnings:
$ gcc main.c -o main-bin -ansi -pedantic -Wall -lm main.c: In function 'main': main.c:6: warning: initialization from incompatible pointer type main.c:9: warning: format '%p' expects type 'void *', but argument 2 has type 'char (*)[4]' main.c:11: warning: format '%p' expects type 'void *', but argument 2 has type 'char **' main.c:12: warning: format '%p' expects type 'void *', but argument 2 has type 'char ***'
(Flags for gcc is because I have to be C89)
Why incompatible pointer types? Is the name of the array a pointer to its first element? Therefore, if s is a pointer to 'a', &s must be char ** , no? And why am I getting other warnings? Do I have to specify pointers ( void * ) to print them?
And at startup, I get something like this:
$ ./main-bin The value of s is: 0xbfb7c860 The direction of s is: 0xbfb7c860 The value of p is: 0xbfb7c860 The direction of p is: 0xbfb7c85c The direction of s[0] is: 0xbfb7c860 The direction of s[1] is: 0xbfb7c861 The direction of s[2] is: 0xbfb7c862
How can the value of s and its direction (and, of course, the value of p ) be the same?
c pointers
alcuadrado Oct 13 '08 at 14:25 2008-10-13 14:25
source share