Error: Missing subscript

Jul 21, 2012 at 12:56pm
Why does the error actually mean in the code:

void mat(int matrix[][]);

But there's no error for this code:

void mat(int matrix[]);

Whats the technical difference?
Jul 21, 2012 at 1:09pm
int matrix[] is an incomplete type when used as a paramter. Use int* matrix instead.

int matrix[][] is meaningless. Use int** matrix instead.

Please note that the array sizes are not passed, you must pass these seperately.

There's some background here: http://www.cplusplus.com/forum/general/70081/#msg373940
Last edited on Jul 21, 2012 at 1:20pm
Jul 21, 2012 at 1:20pm
BTW why is this code correct:

void mat(int matrix[][1]);
Jul 21, 2012 at 2:03pm
To quote the C standard:
An incomplete type may only by used when the size of an object of that type is not needed. It is not
needed, for example, when a typedef name is declared to be a specifier for a structure or union, or
when a pointer to or a function returning a structure or union is being declared. (See incomplete types
in 6.2.5.) The specification has to be complete before such a function is called or defined.

6.2.5.22 goes on to say:
An array type of unknown size is an incomplete type. It is completed, for an identifier of
that type, by specifying the size in a later declaration (with internal or external linkage).

What does that mean?

It means that for:
 
void mat(int param[]);
the size is not needed. It can be treated as an array of int of unspecified size where the size isn't needed. Incrementing a pointer to the next element of the array means adding sizeof(int) to the pointer.

For:
 
void mat(int param[][]);
a 2D array (an array of int[]) is specified, but we don't know the size of int[]. In this case, the size is needed. We don't know how to increment the pointer to an int[].

For:
 
void mat(int param[][1]);
a 2D array (an array of int[1]) is specified. We know how to increment the pointer to the next element, it's sizeof(int) * 1.

Why all this? The background link I posted earlier talks about the history of the treatment of arrays.
Last edited on Jul 21, 2012 at 2:06pm
Jul 22, 2012 at 9:50am
And what if I've to pass matrix[5][5] to void mat(int matrix[][1]); ?
Jul 22, 2012 at 10:22am
Did you read the link I posted earlier?

And why would you try to pass a matrix[5][5] to a void mat(int matrix[][1])? Hopefully you'll get a type mismatch error.
Jul 23, 2012 at 2:48pm
And is there any difference in these two statements?

void mat(int mat[]);

void mat(int mat[20]);
Jul 23, 2012 at 4:15pm
Not really, no. The array size stated here is not used.
Last edited on Jul 23, 2012 at 4:16pm
Topic archived. No new replies allowed.