X,Y passing size for the array in C function
I've declared my function where I want to find the minimum from diagonals [0][0] ... [5][5] and [0][5]... [5][0]. I have the algorithm but my problem is with the actual function header.
I have problem with creating the formal arguments for the function.
I know that we have to pass at least x[][this]
size of array to the function, but I tried various combinations, even
double minimum(int size, double x[][size]){...}
double minimum(double x[][int size]){...}
The first case gives an error when calling:
minimum(10, x[][10])
error: expected expression before ']' token `
The second case gives an error in declaration of function:
error: expected expression before 'int'
Can someone tell what the problem is (or problems are)?
Solution 1:
double minimum(int rowsize,int size, double x[rowsize][size]){...}
or simply
double minimum(int rowsize,int size, double x[][size]){...}
So specifying rowsize
is optional.
But here I guess it is square size x size
so it will be
double minimum(int size, double x[][size]){...}
So you are correct in that.
How to call it?
minimum(10,x)
Solution 2:
If you are dealing with an array of the fixed size 5 then you can declare the function like
double minimum( double x[][5] );
and call it like
minimum( x );
If the size of the array can be changed you can use variable length arrays and declare the function like
double minimum( size_t n, double x[][n] );
of for self-documentation like
double minimum( size_t n, double x[n][n] );
The function can be called like
minimum( 10, x );
or like
minimum( sizeof( x ) / sizeof( *x ), x );