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

前端 未结 3 1309
無奈伤痛
無奈伤痛 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:52

    Your misunderstand lies in what s is. It is not a pointer: it is an array.

    Now in most contexts, s evaluates to a pointer to the first element of the array: equivalent to &s[0], a pointer to that 'H'. The important thing here though is that that pointer value you get when evaluating s is a temporary, ephemeral value - just like &s[0].

    Because that pointer isn't a permanent object (it's not actually what's stored in s), you can't make a pointer-to-pointer point at it. To use a pointer-to-pointer, you must have a real pointer object to point to - for example, the following is OK:

    char *p = s;
    char **p2 = &p;
    

    If you evaluate *p2, you're telling the compiler to load the thing that p2 points to and treat it as a pointer-to-char. That's fine when p2 does actually point at a pointer-to-char; but when you do char **p2 = &s;, the thing that p2 points to isn't a pointer at all - it's an array (in this case, it's a block of 13 chars).

提交回复
热议问题