【发布时间】:2021-09-21 23:42:38
【问题描述】:
此代码来自 CS50 课程 pset4,“过滤器”任务。 概念如下:
- 打开图像文件 (BMP)
- 将所有像素读取到二维数组,其中 1-st lvl 是高度,2-nd lvl 是宽度
代码如下:
// height, width are image`s property
// RGBTRIPLE is an entity (struct) of a pixel
RGBTRIPLE(*image)[width] = calloc(height, width * sizeof(RGBTRIPLE));
...
for (int i = 0; i < height; i++)
{
// Read row into pixel array
fread(image[i], sizeof(RGBTRIPLE), width, inptr);
...
}
我明白:
- 我们创建一个指针 *image
- 我们为二维数组分配了足够的内存:
calloc(height, width * sizeof(RGBTRIPLE))
但是(*image)[width]是什么意思?它不应该是指向 width length 数组的指针吗?如果是这样,为什么稍后我们遍历图像高度并填充 width 长度数组?
Reddit 上的一个人写道:“RGBTRIPLE (*image)[width]声明图像是指向一个或多个数组的指针”。然后它就开始有意义了。 但我不知道这个“...或更多数组”部分在哪里?
【问题讨论】:
标签: c multidimensional-array dynamic-memory-allocation cs50 implicit-conversion