Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why multidimensional array must have bounds when I specify it in the initialization in cpp

Tags:

c++

arrays

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?

like image 845
pio Avatar asked Jul 24 '17 08:07

pio


Video Answer


1 Answers

[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).

like image 113
Martin Bonner supports Monica Avatar answered Sep 28 '22 05:09

Martin Bonner supports Monica