This code is from the CS50 course, pset4, the "Filters" task. The concept is following:
- open an image file (BMP)
- read all pixels to a 2D array, where 1-st lvl is a height, and 2-nd lvl is a width
The code is next:
// 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);
...
}
I understand that:
- we create a pointer *image
- we allocate enough memory for 2d array:
calloc(height, width * sizeof(RGBTRIPLE))
But what (*image)[width] means? Shouldn't it be a pointer to the width length array? If so, why later we iterate through the image height and fill the width length array?
One guy on Reddit wrote: "RGBTRIPLE (*image)[width] declares that image is a pointer to one or more arrays". And it starts making sense then. But I can't get where this "...or more arrays" part?