Why multidimensional array should have bounds when I specify it in initialization in cpp

when trying to initialize a multidimensional array like this:

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

      

I am getting 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 has to know this [2] [3] array because of {}.

I know this is allowed to do as well:

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

      

and in this case indeed the compiler cannot guess what is the 2nd dimension if it is missing, but why not allow the use of [] [] in the first case?

+3


source to share


2 answers


[Shrug] This is how it is.

You can suggest changing the standard to allow this. You will need:



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

It doesn't seem obvious to me that this will have a lot of gotchas, but on the other hand, it is also unclear what benefit it would be (adding extra support for raw arrays is probably pretty small for everyone).

+3


source


C ++ - an array is a contiguous block of cells, lines (in this example) starting from the 3rd, 6th. This is impossible without a size declaration.

The standard / compiler creators want to check initializers against declaration - and not guess from multiple initializers, maybe some with typo errors. I think guessing sizes for over 3.4 dimensions with variable initialization length is too difficult? EDIT: Agree with molbdnilo's comment, compiler simplification



If you have training in Java / C # (or high-level object implementations like std :: vector), you need to think about C style.

EDIT: One dimensional array, declared explicitly or by initialization in both, is very simple. C linear structure (i.e. can be accessed with negative or out-of-index size). To say that true, compiled code does NOT have size dependencies, this knowledge is not used.

+1


source







All Articles