Why must you specify the length of a second array when passing it to a function?

I'm not sure if I phrased my question right, sorry.
I'm having trouble understanding why the first function (help) is wrong.
1
2
3
4
5
6
7
8
9
int a[2][2];

void help(int b[][]);

int main()
{
    help(a[][2]);
    help(a);//why is that wrong?
}
Last edited on
Both of those calls are wrong.
So is the help() function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void help(int b[][]); // <- this is wrong, you cannot have a 2D array without specifying
  // dims of the low order array

void help(int b[][2]);  // <- this would be OK

int main()
{
    help(a[][2]);  // <- this is wrong.  You NEVER specify the array size in brackets like this.  the
       // only time that is done is when you are first creating the array.  If you do it after that
       //  you are not specifying the size, but are accessing an out-of-bounds element.
       //  Furthermore, you can't use an empty bracket [] with an array name post-creation.

       //  I don't know what compiler you were using which let you think this was correct...
       //   ... but it definitely should not have been compiling.


    help(a);  // This works with the  'fixed' help funciton
}



Multidimensional array syntax sucks. Which is why I generally avoid it like the plague.

Further reading:
http://www.cplusplus.com/forum/articles/17108/
Last edited on
Topic archived. No new replies allowed.