如何理解 `int (*pixels)[image->size_x];`?

How to make sense of `int (*pixels)[image->size_x];`?

我的一个朋友让我向他解释以下摘录(为了上下文,我翻译了原始评论和变量名称):

typedef struct {
    int size_x, size_y, shades;
    void * pixels;
} t_image;

int read(FILE * file, t_image * image) {
    // ...
    /* we allocate a sufficiently large array */
    image->pixels = malloc(image->size_x * image->size_y * sizeof(int));
    /* and just now we declare an auxiliary pointer to the array */
    /* of known dimensions, we will access it only through that pointer */
    // MY COMMENT: HERE COME THE WEIRD LINES
    int (*pixels)[image->size_x]; /* pixels in the image struct */
    pixels = (int(*)[image->size_x]) image->pixels; /* we initialise it as we should */
    // ...
}

这是什么,一次声明和取消引用一个指针?它看起来有点像函数指针,但实际上不是,因为它包含 [image->size_x] 而不是 (image->size_x).

// declare a pointer to an array of array of ints of size image->size_x
int (*pixels)[image->size_x];

// assign image->pixels to pixels
// since image->pixels is of type void*, you have to cast it
pixels = (int(*)[image->size_x]) image->pixels;

然后您可以通过

访问图像的行
pixels[row];

并逐行迭代,

pixels++;

最后通过

访问特定像素
pixels[row][col];

// or of the active row
(*pixels)[col];

根据您的评论:

int pixels[image->size_x][] 不等于 int (*pixels)[image->size_x].

int (*pixels)[image->size_x] 等同于 int pixels[][image->size_x].

基本上像char *str[80]等同于char str[][80]
每个长度为 80 个字符的字符串数组。