As the question clearly is about how to access an index of an array whose elements are const char*
,
I believe the below example is an easy and straight forward approach using an enum.
#include <iostream>
using namespace std;
int main()
{
enum StringCodes {
STRING1,
STRING2,
STRING3,
STRING4,
STRING5,
STRING6
};
const char *MyArray[] =
{"string1","string2","string3","string4","string5","string6"};
const char *string1 = MyArray[STRING1];
cout << string1;
return 0;
}
You may want to use another container, such as std::map
which lets you associate a key with your element:
map<string,const char*> myMap;
myMap["string1"] = "string1";
myMap["string2"] = "string2";
cout << myMap["string1"];