passing dynamic string array

Dec 20, 2015 at 11:23pm
I want to be able to dynamically create an array of strings. For example, I might want to create an array of state names. The number of states and string length are unknown until runtime. I'm aware I could accomplish this with a vector, but want to learn the c-like way of doing the same thing.

1
2
3
4
void CreateNames(char ** names)
{
  *(names++) = "Maryland";
}


If this could work, how could I pass a pointer to this method?

I was able to do something similar in this manner.
1
2
 char* arr[25];
CreateNames(&arr[0]);


But here I'm specifying the length of the string to be 25, right?
Last edited on Dec 20, 2015 at 11:24pm
Dec 20, 2015 at 11:29pm
But here I'm specifying the length of the string to be 25, right?

No.

char* arr[25];
This creates an array of 25 char-pointers. Each of these 25 char-pointers is going to point to a string. You've limited yourself to 25 strings, but you've said nothing about the length of those strings.
Dec 21, 2015 at 1:19am
Thanks,

Let's take this a step further. I do not want to specify a fixed size for the number of char pointers I create. This will be determined at runtime. Can I do this?
Dec 21, 2015 at 1:23am
Just wondering, why not use a vector instead? Or do you want to learn dynamic arrays specifically?
Dec 21, 2015 at 1:35am
I'm trying to learn about dynamic arrays. I just don't want to initialize the array with a fixed size. I want the method to to be flexible enough that I don't have to hard code limits, etc.
Last edited on Dec 21, 2015 at 1:36am
Dec 21, 2015 at 2:29am
Ok, here is a tutorial for dynamic arrays, I benefited from it in the past:

http://www.cplusplus.com/doc/tutorial/dynamic/
Topic archived. No new replies allowed.