10

when trying to initialize a multidimensional array like this:

int a[][] = { {1,2,3}, {4,5,6} }; 

I get this error:

error: declaration of iArray as multidimensional array must have bounds for all dimensions except the first 

but I want to understand why, the compiler should know it's a[2][3] array because of the {}.

I know that is allowed also to do:

int a[][3] = {1,2,3,4,5,6}; 

and for that case indeed the compiler can't guess what is the 2nd dimension if it missing, but why not to allow the use of a[][] in the first case?

2
  • 2
    when initializing an array like this int a[5] = { 1,2,3}; you are allowed to omit values in the end. Thats the only reason I could imagine. However, int a[] = {1,2,3}; is allowed. So maybe there is no technical reason, but it just this way because it is this way Commented Jul 24, 2017 at 8:59
  • 3
    I have a feeling that the "why" was lost a long time ago inside Dennis Ritchie's beard. Possibly because that's the valid syntax for function parameters, and applying the same rule everywhere simplifies the compiler. Commented Jul 24, 2017 at 9:12

2 Answers 2

6

[Shrug] That's just the way it is.

You could propose changing the standard to allow this. You would need to:

  • write a specification
  • explain why this is worth adding additional complexity (to both standard and implementations).
  • probably modify an existing implementation to add this as an extension.

It's not obvious to me that this would have many gotcha's, but on the other hand, it's also not obvious how much of a benefit it would be (adding extra support for raw arrays is probably pretty low on everyone's mind).

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

Comments

4

C++ array is continuous block of cells, rows (in this example) starting at 3th, 6th. Without size declaration this cannot be done.

Creators of standard / compiler want to check initialisers against declaration - and not to guess from multiple initialisers, maybe some with typos errors. I think, guessing sizes for over 3,4 dimensions with variable initialisation lengths is too hard? EDIT: agree with molbdnilo comment, compiler simplification

If You have some training in Java/C# (or high level object implementations like std::vector), must think in C style.

EDIT: One dimension array, declared explicitly or by initialisation in both options is very simple C linear structure (i.e. can be accessed at negative or beyond size index). To tell the true, compiled code have NOT dependencies on size, this knowledge is not used.

3 Comments

OP is not asking the compiler to guess the dimension or leave them unspecified, but the dimension are already given in { {1,2,3}, {4,5,6} };, so why they have to be repeated?
You don't have to specify the full number of elements for each row (omitted elements are value-initialised, in this case to zero), so the rule must be at least the row width is taken from the longest row provided, but that makes declarations hard to read unless they're nicely formatted for alignment.
@Useless - And I would rather have an error if there are 99 rows of equal length and one that differs - be it longer or shorter. :-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.