The tricky thing is that people don't think of at first is that you have to go through and initialize each dimension of the array. A lot of times people will try
1 2
// this code is wrong
char *myArray = newchar[rows][cols];
But you have to set up a for-loop and go through and set up each dimension manually.
1 2 3 4 5
// correct way
char **myArray = newchar*[rows];
for( int i = 0; i < rows; ++i ) {
myArray[i] = newchar[cols];
}
Yes, it starts to get tricky when the solution is not linear. So how do you delete[] the new chars, by iterating back through it? If so, could you give me the exact syntax? I've tried your example in my code and it works perfectly, but I'm nor sure if I'm deleting properly. Here's what I'm doing...
1 2 3 4
for(int i = 0; i < rows; i++)
delete[] myArray[i];
delete[] myArray; // for the initial declaration