I try to assign two c-strings at once:
char **pair = {"name","joe"};
but I get error message:
'initializing' : cannot convert from 'const char [5]' to 'char **'
But I think something like that worked for me before?
I try to assign two c-strings at once:
char **pair = {"name","joe"};
but I get error message:
'initializing' : cannot convert from 'const char [5]' to 'char **'
But I think something like that worked for me before?
You declare a pointer-to-pointer but the initializer is an array of pointers:
const char *pair[] = { "name", "joe" };
As noted I added the const
keyword to the declaration. This is because the pointers are pointers to string literals, and literals are constant and can't be changed. Adding that const
helps the programmer not to make changes by mistake to the strings in the array.
Use C++ facilities, it will save you a world of pain:
vector<string> field = { "name", "joe" };
Although maybe you need:
pair<string, string> field("name", "joe");
Or better yet, possibly:
struct Person {
Person(const string& name) : name(name) {}
const string name;
};
Person boss("joe");
Joachim approach creates read-only string. That's how you create an array of const string literals.
Another way,
// MAX_LEN being the max string length.
char pair[][MAX_LEN] = {"name","joe"}; // This makes pair read-writeable,
All answers given here are correct. What to choose depends on context:
If neither 1 or 2, go for Peter Wood's. Note that
vector<string> field = { "name", "joe" };
requires C++11