How do I Dynamically Allocate Memory for a 2d char array.

I understand how to dynamically allocate memory, I am just getting a little bit confused when it comes to doing it for a 2d char array. How do I go about doing this?

Thanks!
-Andrew
Can I ask why it HAS to be 2D? Usually you can get away with using a 1D array for the same purpose. Or is this academic and you don't actually have a project in mind?
Something like this?

1
2
3
4
5
6
7
8
9
10
11
12
//create 2d array
char **arr = new *char[ROWS];
for( int i = 0; i < ROWS; i++ )
  arr[i] = new char[COLS];

//use 2d array
...

//delete 2d array
for( int i = 0; i < ROWS; i++ )
  delete[] arr[i];
delete[] arr;

A better way may be to use a class (or structure) for the memory management.
A vector of vectors works too if you want.
This is for educational purposes. It is part of a big project I am doing for my beginner C++ class. Thanks for all the help.
-Andrew
No problem AndrewGary. Did I answer your question?
Topic archived. No new replies allowed.