为什么会这样
char** p;
char* s = (char*)malloc(6);
strcpy(s,"abcde");
s[2] = 'f';
printf("%s",s);
*p = s;
p[0][2] = 'g';
printf("\n%s",*p);
输出:
abfde
abgde
但这不是
char** p;
char* s = (char*)malloc(6);
strcpy(s,"abcde");
s[2] = 'f';
printf("%s",s);
*p = s;
*p[2] = 'g';
printf("\n%s",*p);
输出:
abfde
abfde
为什么在第一种情况下使用索引访问位置 (p[0][2]) 有效但使用引用 (*p[2]) 无效?
最佳答案
第一个代码有未定义的行为,当 *p = s;
发生时,p
未初始化。因此,它不“工作”;任何事情都可能发生。
也就是说,查看运算符优先级规则。
关于在 C 中使用双重引用更改 char 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46097840/