# Using bidimensional C arrays in a procedure

Hello,

I am now studying C as it’s required for some of my university subjects…

We have studied 1-d arrays in class, but, I’ve decided to save myself some work and move forward, so I’m already studying matrices (bidimensional arrays).

I want to pass an array to a function and compute the average value of each row. If this was to be done with a single array, the function prototype could be:

``````void computesAvg(int v[], int students);
``````

where students actually coincides with the maximum number of elements of v.

My issue is that, when I try to extend this idea, for something like:

``````void computeAvg(int v[][], int classes, int students);
``````

After compiling a program with the above function prototype, I get an error, saying that the type is incomplete or not specified…

However, something like:

``````#define CLASS 7
#define STUD 10

void computeAvg(int v[CLASS][STUD], int classes, int students);
``````

already works… My only issue is that the prototype the teacher specified was the one without any dimensions…

Any clues?

I know arrays are actually changed when passed to procedures/functions, so maybe it has something to do with it and the compiler needs to know the dimensions beforehand?

Bruno

As for the C ANSI standard, it turns out that the compiler needs these dimensions since it can’t guess the “internal” dimension, since in memory, arrays are all represented as 1-D arrays…

I consider my own question answered

The general rule is that, when you are having a `n`-dimensional array, the prototype requires the sizes of (at least) all the `n-1` dimensions, except the first. The first one is optional. The compiler (or whatever software is involved here) can auto compute the size of the first dimension, based on the actual parameter, that is getting passed.

So, when `n = 1`, you need to specify at least`n-1 = 0` dimensional sizes.
When `n = 2`, you must specify at least `n-2 = 1` dimensional size.

In general, the prototype of a function with an `n`-dimensional array as a parameter would look like

``````return-type function-name(type array-name[][S2][S3][S4]...[Sn], type arg2, type arg3, ...);
``````

where S2, S3, …, Sn are the sizes of each dimensions. The size S1, which was supposed to be the size of the first dimension, will be auto calculated.

In the above example,

``````#define CLASS 7
#define STUD 10

void computeAvg(int v[][STUD], int classes, int students);
``````

would also work fine!

1 Like

I marked this answer as Accepted!

It describes exactly the info I managed to found and it’s very well explained

Thank you

Best regards,

Bruno

//