You could use a previous formal argument as the dimension for a later formal argument of a C99 function, see this question.
So the following code is valid:
double
sum (int w, int l, double mat[w][l])
{
double s = 0.0;
for (int i = 0; i < w; i++)
for (int j = 0; j < l; j++)
s += mat[i][j];
return s;
}
If you have declared
double t[5][7];
You can call
double s = sum(5,7,t);
However, you might not want that. A possibility could be to represent a matrix (abstract data type) as a pointer to a struct
ending with a flexible array member:
struct matrix_st {
unsigned w;
unsigned h;
double m[]; // conventionally w*h double numbers
};
You could allocate it with
struct matrix_st *make_matrix (unsigned w, unsigned h)
{
// we might check that w and h are of reasonable size....
// to avoid arithmetic overflow, or excessive memory allocation.
struct matrix_st* pm = malloc(sizeof(struct matrix_st) + sizeof(double)*w*h);
if (!pm) return NULL; // malloc failure
pm->w = w;
pm->h = h;
for (long i=w*h-1; i>=0; i--) pm->m[i] = 0.0;
return pm;
};
for example
struct matrix_st* mat = make_matrix(5,7);
and when you are finished, just call free(mat);
(preferably followed by mat = NULL;
).
You could code the function accessing it as:
inline double matrix_access(struct matrix_st*pm, unsigned i, unsigned j)
{
assert (pm != NULL);
assert (i < pm->w);
assert (j < pm->h);
return pm->m[i*pm->w+j];
}
(practically speaking, since that function is inline
, it can run really fast, nearly as fast as other solutions or answers to your question)
I leave to the reader the exercise of coding other matrix functions (including modification of an element of the matrix, the equivalent of sum
, etc...).