gpt4 book ai didi

c - 关于双指针和三指针/双维数组

转载 作者:太空狗 更新时间:2023-10-29 15:05:38 25 4
gpt4 key购买 nike

所以,我一直在玩 C 指针和指针算法,因为我对它们并不完全满意。我想出了这段代码。

char* a[5] = { "Hi", "My", "Name", "Is" , "Dennis"};
char** aPtr = a; // This is acceptable because 'a' is double pointer
char*** aPtr2 = &aPtr; // This is also acceptable because they are triple pointers
//char ***aPtr2 = &a // This is not acceptable according to gcc 4.8.3, why ?

//This is the rest of the code, the side notes are only for checking
printf("%s\n",a[0]); //Prints Hi
printf("%s\n",a[1]); //Prints My
printf("%s\n",a[2]); //Prints Name
printf("%s\n",a[3]); //Prints Is
printf("%s\n",a[4]); //Prints Dennis

printf("%s\n",*(a+0)); //Prints Hi
printf("%s\n",*(a+1)); //Prints My
printf("%s\n",*(a+2)); //Prints Name
printf("%s\n",*(a+3)); //Prints Is
printf("%s\n",*(a+4)); //Prints Dennis

printf("%s\n",*(*(aPtr2) +0)); //Prints Hi
printf("%s\n",*(*(aPtr2) +1)); //Prints My // ap = a, *ap = *a, *(ap)+1 = *a+1 ?
printf("%s\n",*(*(aPtr2) +2)); //Prints Name
printf("%s\n",*(*(aPtr2) +3)); //Prints Is
printf("%s\n",*(*(aPtr2) +4)); //Prints Dennis

char*** aPtr2 = &a 根据 gcc 4.8.3 是 Not Acceptable ,为什么?

抱歉忘记添加编译器警告:

warning: initialization from incompatible pointer type [enabled by default]

可能不清楚我想说什么,所以我不得不添加这个链接:

注意注释掉的行。

最佳答案

a 是 5 个指针的缓冲区的地址并且是不可变的(即它是一个固定的指针)。如果你允许

char ***aPtr2 = &a;

然后

*aPtr2 = &a[3];

实际上会修改地址a(这是禁止的)。

关于c - 关于双指针和三指针/双维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30579784/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com