3

I'm still confused. Can I do this?

int x[y[3]]

Array within array?

Is that the correct format?

Thank you!

David G
  • 94,763
  • 41
  • 167
  • 253
Amadeus
  • 67
  • 1
  • 5

3 Answers3

6

int x[y[3]]; is valid if y[3] is an integral constant expression, and it will declare an array of y[3] elements. Otherwise it’s invalid.


If you are looking for a 2D array, try the following:

#include <array>
std::array<std::array<int, 3>, 4> x;

If you want less pain to go through, you can look at Boost.MultiArray, as suggested by Cat Plus Plus.

0

Even with the assumption that y[3] is of an integer type (otherwise it makes no sense), VLA (variable length arrays) are not supported in c++. They are part of C99, but not c++. Therefore, your code is not good.

Some compilers do support VLA, but only as an extension.

BЈовић
  • 62,405
  • 41
  • 173
  • 273
-6

It would be something like this

int[,] myArray = new int[1][2];
myArray[0][1] = 1;
myArray[1][1] = 1;
Ben
  • 5,627
  • 9
  • 35
  • 49