Reputation: 381
I currently have a lot of character arrays that store simple character strings in English for display. I also have pointers to these character arrays.
char helloAr[20] = "Hello";
char timeAr[20] = "Time";
char dogAr[20] = "Dog";
char* helloPtr = helloAr;
char* timePtr = timeAr;
char* dogPtr = dogAr;
I am adding more character arrays in a different language, French to begin with.
char helloArFr[20] = "Bonjour";
char timeArFr[20] = "temps";
char dogArFr[20] = "chien";
If the user selects French I plan to change the address that all my pointers (currently pointing at the English character arrays) point to, so they now point to the French character arrays. I assume I can simply assign a new values to these pointers to do this,
helloPtr = helloArFr;
timePtr = timeArFr;
dogPtr = dogArFr;
however my actual code will have a lot of arrays so I wanted to use a loop to do this rather than lots of statements like the one above.
To do this I plan to create an array of character pointers to the addresses of my character arrays.
char* charArrAddresses [NUMBER_OF_TEXT_ARRAYS]=
{
&helloAr,
&timeAr,
&dogAr,
&helloArFr,
&timeArFr,
&dogArFr,
};
I also plan to store all the pointers in an array so that I can reference them by their location in the array in my single statement, but this is where I need some help as I am unsure on how to do this. Something like this maybe?
char ** langPtrs[NUMBER_OF_LANG_POINTERS]=
{
helloPtr,
timePtr,
dogPtr,
};
In the single statement inside the loop I will use the selected language and the number of char arrays to calculate the correct index for charArrAddresses
, lets call this X
for now. I will use the value of my loop (say i
) to index my langPtrs
array. So in my single want to say something like,
for(i = 0; i<NUMBER_OF_LANG_POINTERS; i++)
{
langPtrs[i]= charArrAddresses[X];
}
I am trying to assign the address of the character array stored in index X
of charArrAddresses
to the pointer in index i
in the langPtrs
array. I just need help on exactly how to declare the langPtrs
array and on how to write my single statement above.
Upvotes: 3
Views: 186
Reputation: 224437
Rather than placing the strings for all languages in a single array, make a 2D array of strings and languages. If you don't plan on modifying any of these strings, you can make them string literals instead of arrays.
enum langs {
LANG_ENGLISH,
LANG_FRENCH,
NUMBER_OF_LANGS
};
enum strings{
STR_HELLO,
STR_TIME,
STR_DOG,
NUMBER_OF_STRINGS
};
const char *langPtrs[NUMBER_OF_LANGS][NUMBER_OF_STRINGS]=
{
{ "Hello", "Time", "Dog" }
{ "Bonjour", "temps", "chien" }
};
Since the values of the symbols in an enum
start at 0 and increment by 1 for each subsequent symbol, you can use them an array indexes. The last member of each enum
, which does not correspond to an actual element in the array, can be used as the size of the array.
So if you want to, for example, print the string for "time" in French, you would use:
printf("%s\n", langPtrs[LANG_FRENCH][STR_TIME]);
If you want to always print string from whatever the "current" language is, you can create a pointer to the subarray for the current language:
const char **currentLang = langPtrs[LANG_FRENCH];
Then you can use that:
printf("%s\n", currentLang[STR_TIME]);
EDIT:
If you want to keep helloPtr
, timePtr
, and dogPtr
, and you want to set them based on the current language, you can put their addresses in another array:
const char **usedInClassesPtrs[NUMBER_OF_STRINGS] = {
&helloPtr,
&timePtr,
&dogPtr
}
Then loop through the array, dereferencing each element to give you the actual pointers you want to change, and assign them a string from the 2D language array based on the current language as follows:
for (i=0; i<NUMBER_OF_STRINGS; i++) {
*usedInClassesPtrs[i] = langPtrs[currentLang][i];
}
Upvotes: 9
Reputation: 1
The @debush answer should work quite well.
From your example i assume that only 1 language is active at a time in your code. For that, i suggest to use a resource approach to manage your multi-language support. You can basically load the corresponding resource file of a language and store it in std::vector<std::string>
, std::map<const enum/id/etc., std::string>
or any similar data structure. This can be done either on the fly or at initialization stage depending on your need. In your code you save a pointer to this data structure std::vector<std::string>* ptr
to refer to currently loaded language by invoking (*ptr)[index]
.
Upvotes: 0