How doesn't this work?

I know these are strings:
1
2
char* s1;
char s2[];

But how to make an array of strings?
I have tried:
1
2
3
4
//These
char* s[]; //This won't even compile
char** s; //This will compile but I don't know how to use it...
char* s[100]; //This works but I don't want to waste memory for nothing. 

Any help?
You need to specify the size of the array as you do with char* s[100];.

char** s; can work but then you have to allocate the array yourself.
1
2
3
char** s = new char*[100];
// ...
delete[] s; // free the memory when you no longer need it 


Easier way is to use std::vector instead of array and std::string instead of char* (aka c string).
std::vector<std::string> s;
Last edited on
How about just using the string class?

1
2
3
4
5
#include <string>

const int STR_ARRAY_MAX = 3;

string my_string[STR_ARRAY_MAX] = {"Hello", "World", "String"};


The string in a string class is just a null-terminated character array.
I thought I could use a variant array like in int a[]; but you say that I have to specify the size of an array. I'll do what you said. Thanks.
Topic archived. No new replies allowed.