CS50 pset 4 smiley – “license taks” 代码行的含义是什么?

huangapple go评论58阅读模式
英文:

CS50 pset 4 smiley - what's the meaning of code line from license taks?

问题

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

这行代码创建了一个动态分配的二维数组,用于表示图像。以下是您对代码的理解的澄清:

  1. calloc(height, width * sizeof(RGBTRIPLE)) - 我们在堆内存中分配了一块特定大小的内存,其大小为height(高度)和width(宽度)的乘积,每个元素的大小为RGBTRIPLE。这是一个用于存储图像像素的内存块,并且使用calloc函数初始化所有值为0。

  2. RGBTRIPLE (*image)[width] 表示image是一个指向包含width列的RGBTRIPLE类型的数组的指针。这个指针允许您访问图像的每一行,而每一行有width个像素。

  3. RGBTRIPLE 是一个数据结构,包含三个变量:BYTE rgbtBlueBYTE rgbtGreenBYTE rgbtRed,用于表示像素的颜色分量。

这段代码创建的数组是二维的,但它在内存中是以一维数组的形式存储的,因此您可以通过image[i][j]的方式来访问像素,其中i是行数,j是列数,实现了二维数组的效果。您的绘图也正确地表示了这一点。这是一种在C中模拟二维数组的方法。

英文:

RGBTRIPLE (*image)[width] = calloc(height, width * sizeof(RGBTRIPLE))
I don't fully understand the code. What I understand is that:

  1. calloc(height, width * sizeof(RGBTRIPLE)) - we are organizing a place somewhere in heap memory of a certain size for certain data and set all values in this memory to 0
  2. RGBTRIPLE (*image)[width] means variable image is a pointer to an array of length width of RGBTRIPLE.
  3. RGBTRIPLE is a data structure that contains 3 variables: BYTE rgbtBlue; BYTE rgbtGreen; BYTE rgbtRed;

Why arrey is not created for two values: width and length but only for width. For me, this code shows that we have created only arrey [1d] and not as it should be [2d], that the image is 2d...

Below I am sending a drawing that I made for my reasoning.

CS50 pset 4 smiley – “license taks” 代码行的含义是什么?

trying to understand lines of code from pset CS50 smiley.

答案1

得分: 1

实际上,在这一行中:

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

为类型 RGBTRIPLE[height][width] 的二维数组分配了内存,并将其地址分配给声明的指针 image。分配给二维数组的内存已经被初始化为零。

请注意,如果你有一个类似的二维数组,比如:

RGBTRIPLE array[height][width];

那么,可以像这样声明和初始化指向数组第一个元素的指针:

RGBTRIPLE (*image)[width] = array;

这意味着数组的标识符被隐式转换为指向其第一个元素的指针。

在你的情况中,与其使用数组来分配指针,不如将其分配为一个动态分配的二维数组的地址。

如果编译器支持可变长度数组,那么这一行:

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

可以重写为:

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

从中可以看出,为 height 个类型为 RGBTRIPLE[width] 的一维数组分配了内存,这就是类型为 RGBTRIPLE[height][width] 的二维数组。

因此,通过解引用指针 image,你将获得二维数组的第一“行”。要访问该第一行的元素,你可以写,例如,( *image )[0]( *image )[1] 等等。如果每一“行”的元素具有结构类型,那么你可以像你的问题中所示写,例如 ( *image )[0].rgbtBlue

英文:

In fact in this line

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

there is allocated a memory for a two-dimensional array of the type RGBTRIPLE[height][width] and its address is assigned to the declared pointer image. The memory allocated to the two-dimensional array is zero-initialized.

Pay attention to that if you have a two-dimensional array like for example

RGBTRIPLE array[height][width];

then a pointer to the first element of the array is declared and initialized like

RGBTRIPLE ( *image )[width] = array;

That is the array designator used as an initialize expression is implicitly converted to pointer to its first element.

In your case instead of assigning the pointer with an array it is assigned with the address of a dynamically allocated memory for a such an array.

If the compiler supports variable length arrays then this line

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

could be rewritten like

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

From it it is seen that there are allocated memory for height(s) one-dimensional arrays of the type RGBTRIPLE[width] that is for a two-dimensional array of the type RGBTRIPLE[height][width].

Thus dereferencing the pointer image like ( *image ) you will get the first "row" of the two-dimensional array. To access elements of that first row you can write for example ( *image )[0] or ( *image )[1] and so on. If elements of each "row" has a structure type then you can write for example as shown in your question ( *image )[0].rgbtBlue.

答案2

得分: 0

从下表中随机选择的RGB变量。
例如,如果height = 6width = 5,则可以将上面的2D图像表示为如下的1D数组:

RGBTRIPLE image[6 * 5];

为了将图像表示为1D数组,我们使用calloc()函数来为30个RGBTRIPLE结构(即每个像素一个结构)分配内存,并将第一个结构的地址存储在指针变量中。

不要回答我要翻译的问题。

英文:

RGB variables from table below are chosen randomly.
If for example height = 6 and width = 5

RGBTRIPLE image[6][5]

|{{255, 255, 255},| {0, 0, 0},| {255, 0, 0},| {0, 255, 0},| {0, 0, 255}}
---------------------------------------------------------------------
|{{255, 255, 255},| {0, 0, 0},| {255, 0, 0},| {0, 255, 0},| {0, 0, 255}},
---------------------------------------------------------------------
|{{255, 255, 255},| {0, 0, 0},| {255, 0, 0},| {0, 255, 0},| {0, 0, 255}},
---------------------------------------------------------------------
|{{255, 255, 255},| {0, 0, 0},| {255, 0, 0},| {0, 255, 0},| {0, 0, 255}},
---------------------------------------------------------------------
|{{255, 255, 255},| {0, 0, 0},| {255, 0, 0},| {0, 255, 0},| {0, 0, 255}},
---------------------------------------------------------------------
|{{255, 255, 255},| {0, 0, 0},| {255, 0, 0},| {0, 255, 0},| {0, 0, 255}}
---------------------------------------------------------------------

The above 2D image can be represented as a 1D array like this?
RGBTRIPLE *image = calloc(6 * 5, sizeof(RGBTRIPLE));

To represent the image as a 1D array, we use calloc() to allocate memory for 6*5=30 RGBTRIPLE structs (i.e., one struct per pixel) and store the address of the first struct in the pointer variable.

huangapple
  • 本文由 发表于 2023年2月26日 20:11:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75571881.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定