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 anint
) and add1
to it. Return the result (2
)
但是,*(a + 1)
表示...
Add
1
to the pointera
and dereference it, i.e., get the value at the addressa + sizeof(int[2])
.
“值* 恰好是第二个数组的第一个元素。记住;将 n 添加到指针类型会使地址前进 n 个元素。在这种情况下,每个元素都是一个 int
数组,它有两个自己的元素。
这也应该回答接下来的两个问题。阅读完运算符优先级后,开始学习 pointer arithmetic .
关于无法理解 c 中二维数组中的寻址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12152772/