arrays - 这里的“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]
?从左到右?任何帮助是极大的赞赏!!!
解决方案
或者您可以解释RGBTRIPLE(*image)[width]
为RGBTRIPLE (*image)[width]
. 哪里RGBTRIPLE
可能是typedef
ed 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);
如果你不在image
ie周围加上括号RGBTRIPLE *image[width]
,它将是array of pointers
. image
因此,如果您希望它是一个,则必须将括号括起来pointer to array
。
只需将其解释为image is a pointer to an array of width elements of type RGBTRIPLE
.
推荐阅读
- regex - 正则表达式在记事本++中工作,但不在python中
- r - 将整个代码存储在操作按钮中
- java - Spring @Autowired 是否为每个 bean 创建新的参数实例?
- html - 使 div 可滚动是内容超过视口的高度
- django - django-sass-processor:加载 sass_tags 并加载静态
- email - 通过另一个 SMTP 服务器问题从 Gmail 发送邮件
- c# - 如何使用 asp.net-mvc 应用程序使用 python 机器学习模型脚本
- c# - 使用滑动控件播放动画和 transform.position 问题 Unity
- c# - 有没有办法在对话更新中快速选择并告诉选择会做什么?
- angular - 如何只生成 API 的接口?