我现在正在学习c,正在研究指针。我已经阅读了一些示例,并且想到了这个:
#include <stdio.h>
int main ()
{
float balance[10][5];
float *p;
p = (float *) balance;
*(p + 16) = 200.0; //init the balance[3][1] with the value 200.0
printf("%f\n", balance[3][1]);
return 0;
}
我的问题是这样的。为什么我必须用 (float *)
来平衡?这是因为数组是二维的吗?那么指针也是二维的?所以我必须将它转换为一维?
最佳答案
Why i have to cast the balance with a (float *) ?
这是因为 p
的类型是 float *
而 balance
的类型是 float (*)[5]
衰减到指针后。
is this because the array is 2 dimensions?
是的。
so the pointer is also 2 dimensions?
balance
衰减到 float (*)[5]
。
so i have to transform it to a 1 dimension ?
是的。
关于c - 指向二维数组的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19479260/