Why is a pointer to a pointer incompatible with a pointer to an array?

前端 未结 3 1312
無奈伤痛
無奈伤痛 2021-02-05 18:24

OK, I\'m having trouble understanding pointers to pointers vs pointers to arrays. Consider the following code:

char s[] = \"Hello, World\";
char (*p1)[] = &s         


        
3条回答
  •  故里飘歌
    2021-02-05 18:49

    Here's the sample that works, plus printouts of pointer addresses to make things simple to see:

    #include 
    char s[] = "Hello, World";
    char (*p1)[] = &s;
    char *p2 = (char*)&s;
    
    int main(void)
    {
       printf("%x %x %x\n", s, p2, *p2);
       printf("%x\n", &s);    // Note that `s` and `&s` give the same value
       printf("%x\n", &s[0]);
       printf("%c\n", **p1); 
       printf("%c\n", *p2);
    }
    

提交回复
热议问题