incrementing an array of pointers in C

后端 未结 5 1935
滥情空心
滥情空心 2020-12-20 20:01

this is probably an essentially trivial thing, but it somewhat escapes me, thus far..

char * a3[2];
a3[0] = \"abc\";
a3[1] = \"def\";
char ** p;

相关标签:
5条回答
  • 2020-12-20 20:07

    Try

    char *a3Ptr = a3;
    printf("%p - \"%s\"\n", a3, *(++a3Ptr));
    

    In C, a char array[] is different from char*, even if you can use a char* to reference the first location of an array of char.

    aren't both "p" and "a3" pointers to pointers?

    Yes but a3 is constant. You can't modify it.

    0 讨论(0)
  • 2020-12-20 20:20

    a3 is a constant pointer, you can not increment it. "p" however is a generic pointer to the start of a3 which can be incremented.

    0 讨论(0)
  • 2020-12-20 20:23

    You can't assign to a3, nor can you increment it. The array name is a constant, it can't be changed.

    c-faq

    0 讨论(0)
  • 2020-12-20 20:28

    You cannot increment or point any char array to something else after creating. You need to modify or access using index. like a[1]

    0 讨论(0)
  • 2020-12-20 20:30

    a3 is a name of an array. This about it as a constant pointer.

    You cannot change it. You could use a3 + 1 instead of ++a3.

    Another problem is with the use of "%s" for the *(++a3) argument. Since a3 is an array of char, *a3 is a character and the appropriate format specifier should be %c.

    0 讨论(0)
提交回复
热议问题