c - 如何修复 C 中数组指针的打印错误(段错误)?

标签 c

我刚开始学习 C 中的指针。我发现了一些特别的东西,并得到了关于这些代码的错误,你能帮我找出为什么 No.9 会出现“Segmentation fault”错误吗?

#include<stdio.h>

int main() {
    int a[] = {10, 20, 30, 40, 50};
    int *p[] = {a, a+1, a+2, a+3, a+4};
    int **pp = p;
    int ***ppp = &pp;

    printf("\n === Part1 === \n\n");
    printf(" 0. %p\n", a);
    printf(" 1. %p\n", *p);
    printf(" 2. %p\n", *pp);
    printf(" 3. %p\n", **ppp);

    printf("\n === Part2 === \n\n");
    printf(" 4. %d\n", *p[0]);
    printf(" 5. %d\n", *pp[0]);
    printf(" 6. %d\n", **ppp[0]);

    printf("\n === Part3 === \n\n");
    printf(" 7. %d\n", *p[3]);
    printf(" 8. %d\n", *pp[3]);
    printf(" 9. %d\n", **ppp[3]);

    printf("\n");

    return 0;
}

最佳答案

这与运算符优先级有关。 []* 绑定(bind)得更紧密,所以 **ppp[3] 表示 **(ppp[3]),它不会做你想做的事。

我想你想要 (**ppp)[3]

关于c - 如何修复 C 中数组指针的打印错误(段错误)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55499061/

相关文章:

c - 了解此函数中的位运算

c++ - lseek 在 Windows 中使用 2 GB 文件

c - If 语句条件的问题

从 C 调用 Haskell,得到 "multiple definition of main"链接器错误

c - 如何从c中的sock结构中获取ip地址?

c - 对 _mm_crc32_u64 的 undefined reference

c++ - C/C++ 编译器对条件语句进行了多少优化?

不明白为什么会产生这个输出

c - 读取大型文本文件并在 C 程序中按字母顺序快速排序

c - 为什么复合位移不将前导半字节设置为 0?