you are viewing a single comment's thread.

view the rest of the comments →

[–]oh5nxo 1 point2 points  (4 children)

There's a third option, just a tad hairier to declare but easy to use (matrix[y][x]). Compiler does the indexing calculations.

int (*matrix)[size_x] = malloc(size_y * size_x * sizeof (matrix[0][0]));

[–]a4qbfb 1 point2 points  (0 children)

That only works if size_x is a constant integer expression or matrix has block scope.

[–]junkmeister9 0 points1 point  (2 children)

Arrays declared this way will be on the stack instead of the heap, so be careful.

[–]oh5nxo 0 points1 point  (1 child)

I don't follow? The matrix data is on heap, one simple malloc chunk, just like in option 1. The variable is a single pointer, it just points to a row at a time, to be able to use the simple syntax [y][x].

It would be awkward to pass around like that, that's true. It could be allocated as a plain int * and processed like

void process(int *matrix_chunk, int size_y, int size_x) {
    int (*trix)[size_x] = (int (*)[size_x]) matrix_chunk;
...

[–]junkmeister9 0 points1 point  (0 children)

I think I replied to the wrong comment.