2

Why does this work:

void SomeFunction(int SomeArray[][30]);

but this doesn't?

void SomeFunction(int SomeArray[30][]);

2 Answers 2

2

Because, when passing an array as an argument the first [] is optional, however the 2nd onwards parameters are mandatory. That is the convention of the language grammar.

Moreover, you are not actually passing the array, but a pointer to the array of elements [30]. For better explanation, look at following:

T a1[10], a2[10][20];
T *p1;    // pointer to an 'int'
T (*p2)[20];  // pointer to an 'int[20]'

p1 = a1;  // a1 decays to int[], so can be pointed by p1
p2 = a2;  // a2 decays to int[][20], so can be pointed by p2

Also remember that, int[] is another form of int* and int[][20] is another form of int (*)[20]

Sign up to request clarification or add additional context in comments.

2 Comments

"int[] is another form of int* and int[][20] is another form of int (*)[20]" -- Only in parameter lists. In normal declarations, int[] is an array of however many ints are in its initialization list, likewise with int[][20].
@BenjaminLindley, you are correct. My answer was in the context of the argument passing only.
2

Intuitively, because the compiler cannot compute a constant size for the elements of the formal in the second declaration. Each element there has type int[] which has no known size at compile time.

Formally, because the standard C++ specification disallow that syntax!

You might want to use std::array or std::vector templates of C++11.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.