Declare char *[]

What is the best way to declare the following as a pointer, at least with regard to the first array...

char chStr[MAX_PATH][MAX_PATH];

IOW, I don't care if the second array reamains MAX_PATH, but what if I want the first array to be dynamic?

Perhaps if you know several combinations you could show them.

I'm thinking along the lines of an int *. For example, I get the count of a listbox, then I do the following with an int...

int *iBuff = new int[iCount];
...do stuff
delete[] iBuff;

So what is the best way to do something similar with type char?
Last edited on
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 = new char[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 = new char*[rows];
for( int i = 0; i < rows; ++i ) {
	  myArray[i] = new char[cols];
}

Last edited on
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 
Last edited on
You are doing it perfectly.
Thanks. I appreciate the help.
Topic archived. No new replies allowed.