How doesn't this work?
Feb 4, 2012 at 1:43pm UTC
I know these are strings:
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?
Feb 4, 2012 at 1:48pm UTC
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 Feb 4, 2012 at 1:51pm UTC
Feb 4, 2012 at 1:50pm UTC
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.
Feb 4, 2012 at 2:04pm UTC
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.