无法理解 c 中二维数组中的寻址

标签 c arrays 2d

main()
{
    int a[3][2] = { {1,2},{3,4},{5,6}};
    for(int i=0;i<3;i++)
        for(int j=0;j<2;j++)
    {
        printf("%d", a[i][j]);
        printf("\t %d\n", &a[i][j]);
    }
    printf("\n%d", *(a+1));
    printf("\n%d", *a+1);
}

the output of *(a+1) is different from *a+1.

*(a+1) is pointing to 3 rd element whereas

*a+1 is outputting the 2nd value

最佳答案

the output of *(a+1) is different from *a+1.

是的,由于 operator precedence . *a + 1 表示...

Dereference a (which returns an int) and add 1 to it. Return the result (2)

但是,*(a + 1) 表示...

Add 1 to the pointer a and dereference it, i.e., get the value at the address a + sizeof(int[2]).

“值* 恰好是第二个数组的第一个元素。记住;将 n 添加到指针类型会使地址前进 n 个元素。在这种情况下,每个元素都是一个 int 数组,它有两个自己的元素。

这也应该回答接下来的两个问题。阅读完运算符优先级后,开始学习 pointer arithmetic .

关于无法理解 c 中二维数组中的寻址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12152772/

相关文章:

c - 二维动态数组未初始化 - 函数 C 的参数

python - python 中有孔的三角测量

c - 用串口编程C读取多个传感器

java - 在java中使用哈希码比较两个大字符串?

c - C 中的内存划分

arrays - 创建最小成本数组

javascript - 无法使函数声明起作用

java - 最大限度地减少 gme 中的 AI 使用

c - 我希望我的代码按照字符串出现的顺序打印字符的频率

c - 如何在不使用内置malloc函数的情况下实现enque函数?