【发布时间】:2021-05-15 15:36:21
【问题描述】:
我了解一维数组指针的工作原理,但我不明白二维数组指针的工作原理。
我知道array1[0] == *(array1 + 0),但我不知道如何在二维数组中使用这种形式。 我发现如果我使用双指针或'[]'(我不知道这是什么),那么它可以工作。
-
如何使用指针访问多维数组?
-
可以使用双指针或'[]'吗?如果是,为什么?
-
为什么 *(2Darrayname + number) 不起作用?
-
是不是因为腐烂了? (我不知道腐烂的东西)
编辑)我已经检查了这些 What is array to pointer decay? Pointers in C: when to use the ampersand and the asterisk? Accessing multi-dimensional arrays in C using pointer notation
int main()
{
int array1[5];
int array2[5][5];
f1(array1);
f2(array2);
}
int f1(int *p1)
{
for(int i = 0; i<5 ; i++)
{
printf("%d", *(p1+i)); //this is ok
}
}
int f2(int (*p2)[5])
{
for(int i = 0; i<5 ; i++)
{
printf("%d", *(p2+i)); //this one makes error, and gcc says it is 'int *'
printf("%d", *(p2+i)[1]); //and this one is ok for some reason
printf("%d", **(p2+i)); //this one too. why is this ok??
}
}
【问题讨论】: