Matrix of Strings

No clear (to me) material in the web. Hence I am posting here.

I must allocate memory for a matrix (2D array) of strings during runtime. Here is my code:

string *matrix; //must become matrix[r][nc]
int r, nc;
...
matrix = new string[r][nc];

where r and nc are calculated during runtime.

The compiler is not happy at all with the allocation (new) statement.
But is very happy if I limit matrix to be 1D array, like matrix = new string[r];

What is the correct syntax for the 2D allocation?

Thanks.
Last edited on
Try a double pointer
string **matrix;
I'm uncertain if that will work. I pulled that answer from here
http://www.cplusplus.com/forum/general/8068/
Negative. Does not work and I believe ** means "pointer to pointers".
1
2
3
T **matrix=new T*[width];
for (int i=0;i<width;i++)
    matrix[i]=new T[height];
Thanks to everyone! Very much appreciated.
Topic archived. No new replies allowed.