c - 指向二维数组的指针

标签 c pointers

我现在正在学习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/

相关文章:

c - 创建多个具有互斥锁和不同生命周期的线程

c函数指针在运行时传递参数

c - 如何在 C 中生成一系列具有唯一数字的数字?

c - 如何将函数和变量参数列表传递给 C 中的另一个函数?

c - 为什么这段代码通常可以运行但有时会产生Segmentation Fault?

c - C中负数的按位运算

c++ - C++ 获取基类指针的方法

C:交换两个指针值

pointers - 更改通过引用传递的结构的值

C中字符串大写