How to pass a double array of function to C?

I have been trying to solve this problem all day:

How to pass a double array to a function?

Here is an example:

int matrix[5][2] = { {1,2}, {3,4}, {5,6}, {7,8}, {9,10} }; 

And I want to pass this matrix to a function called eval_matrix ,

 void eval_matrix(int ?) { ... } 

I can not understand what should be instead ?

Can someone help me with this problem?

I know that an array can be passed as a pointer, but what about a double array (or a triple array?)

Thanks, Boda Sido.

+4
source share
2 answers

To be used as an array, the compiler must know the internal step of the array, so it also:

 void eval_matrix( int m[5][2] ) { ... 

or

 void eval_matrix( int m[][2], size_t od ) { ... /* od is the outer dimension */ 

or simply:

 void eval_matrix( int* p, size_t od, size_t id ) { ... /* ditto */ 

In any case, this is syntactic sugar - the array decomposes into a pointer.

In the first two cases, you can refer to the elements of the array, as usual, m[i][j] , but in the third case you will need to compensate for it as p[i*id + j] .

+5
source

You should not pass the entire matrix, instead you should pass a pointer, however you should pass the size too ... so I would do this, assuming that these are always pairs [2].

 struct pair { int a, b; }; struct pair matrix[] = { {1,2}, {3,4}, {5,6}, {7,8}, {9,10} }; void eval_matrix(struct pair *matrix, size_t matrix_size) { ... } eval_matrix(matrix, sizeof(matrix) / sizeof(struct pair); 
-1
source

Source: https://habr.com/ru/post/1315293/


All Articles