Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

How do I declare a 2d array in C++ using new?

How would I announce a 2d array utilizing new?

Like, for an "ordinary" array I would:
int ary = new int[Size]

but

int
ary = new int[sizeY][sizeX]


a) doesn't work/compile and b) doesn't succeed what:
int ary[sizeY][sizeX]
by

2 Answers

akshay1995

int ary = new int[sizeX*sizeY];

// ary[i][j] is then rewritten as
ary[i
sizeY+j]
RoliMishra
You can use the following:

int ary = new int[sizeX * sizeY];


Then you can access elements as:

ary[ysizeX + x]


Don't forget to use delete[] on ary.

Login / Signup to Answer the Question.