首页 > 解决方案 > 这里的“2d_array”是指向另一个具有“width”元素数量的整数数组的指针吗?

问题描述

编码:

int (*2d_array)[width] = calloc(height, width * sizeof(int));

下面是来自在线课程的分发代码,其中 RGBTRIPLE 是一个结构。有人说image 是一个指向长度为 RGBTRIPLE 的数组的指针。所以2d_array这里还有一个指向另一个具有width元素数量的整数数组的指针?

RGBTRIPLE(*image)[width] = calloc(height, width * sizeof(RGBTRIPLE));

我只是对这种语法感到非常困惑。括号如何改变图像的含义?我应该如何解释RGBTRIPLE(*image)[width]?从左到右?任何帮助是极大的赞赏!!!

标签: arrayscpointers

解决方案


或者您可以解释RGBTRIPLE(*image)[width]RGBTRIPLE (*image)[width]. 哪里RGBTRIPLE可能是typedefed struct

例如

typedef struct {
  // data members
}RGBTRIPLE;

因此, inRGBTRIPLE (*image)[width]image指向用户定义数据类型数组的指针RGBTRIPLE,类似于指向类型数组int (*ptr)[width]的指针。int

注意:在 C 语言中,您只能声明以下划线或字母开头的变量名。int (*2d_array)[width]据我所知,这是错误的。


我假设2d_array_2dArray.

所以_2dArray这里还有一个指向另一个具有width元素数量的整数数组的指针?

是的

括号如何改变图像的含义?我应该如何解释RGBTRIPLE(*image)[width]

这些被称为复杂指针。例如pointer to array == int (*ptr)[LEN];pointer to function == int (*ptr)(//function arguments);

如果你不在imageie周围加上括号RGBTRIPLE *image[width],它将是array of pointers. image因此,如果您希望它是一个,则必须将括号括起来pointer to array

只需将其解释为image is a pointer to an array of width elements of type RGBTRIPLE.


推荐阅读