What are the parentheses doing?
As Blauelf explained, they are changing order of operations. I didn't full grasp why this was needed though until trying to compile without them.
filter.c:78:15: error: variable-sized object may not be initialized
RGBTRIPLE*image[width] = calloc(height, width * sizeof(RGBTRIPLE));
They ensure *image creates a ptr called image, and [width] is treated separately as an array dimension. Still don't fully understand what is being attempted without them, but I now understand their necessity...
My main point of confusion was that I understood this works totally fine...
int n = 4;
int *d_arr = malloc(sizeof(int) * n);
for (int i = 0; i < n; i++)
{
d_arr[i] = i;
printf("1d: [%i] is %i\n", i, d_arr[i]);
}
So I assumed that this would also work fine...
int n = 4;
int *dd_arr = malloc(sizeof(int) * n * n);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
dd_arr[i][j] = i + j;
printf("2d: [%i][%i] is %i\n", i, j, dd_arr[i][j]);
}
}
}
But it does not, and won't compile with
2d_arr.c:12:16: error: subscripted value is not an array, pointer, or vector
dd_arr[i][j] = i + j;
~~~~~~~~~^~
2d_arr.c:13:53: error: subscripted value is not an array, pointer, or vector
printf("2d: [%i][%j] is %i\n", i, j, dd_arr[i][j]);
You have to have line two as
int (*dd_arr)[n] = malloc(sizeof(int) * n * n);
Which brings me to my understanding of the following...
Why is width being used, and not height (isn't each element in the
array a row in the image, not a column?)
Best as I understand it, *dd_arr can be treated like an array out of the box, but since a 2d array is really an array OF arrays, you need to let the compiler know this so you can operate on that nested level (or another way, "image" is a pointer to an array that contains "width" number of arrays of RGBTRIPLES)
^^ Is that right?
And in the original question it's width not height, because height is the length of the outermost array implied by the *image, which doesn't need to be specified.
Why are there no spaces?
As Blauelf explained, not functional, only stylistic choice (albeit confusing to me at the time).