ICookixz
ICookixz

Reputation: 21

How do I add a const char * array item to a const char * string?

I am using ImGui and am trying to add an array item to my const char * so that I can display the chosen item. How do I do it? It shows random letters and not what I want.

static const char* FruitsList[] = { "Mango", "Apple", "Pear", "Bannana"};
indexFruit = 2;
const char * chosenFruitText = "Chosen Fruit"  + char(FruitsList[indexFruit]);
ImGui::ListBox(chosenFruitText , &indexFruit , FruitsList, IM_ARRAYSIZE(FruitsList));

Upvotes: 0

Views: 1487

Answers (2)

foamycoffee
foamycoffee

Reputation: 37

There are no character string operations in the C programming language, not like what your code suggests that you expect. In the expression "const char* ChosenFruit = ...", you are telling the compiler that you want to add the address of the array containing "Chosen Fruit" with the address of the third string in FruitsList (which you truncated to the size of a 'char' because of the typecast), and assign the result to ChosenFruit. ChosenFruit just points to some far away place in memory that most likely does not contain printable characters.

To concatenate two strings in C, you must allocate a new array big enough to hold the result, and copy the characters of the two strings explicitly. You can use the 'strcpy()' and 'strcat()' functions to do the copying:

const char* s1 = "Hello ";
const char* s2 = "world!";
char s3[32]; /* big enough to hold s1 and s2 */
strcpy( s3, s1 ); /* copy s1 to s3 */
strcat( s3, s2 ); /* copy s1 to end of s3 */

You can do the same in C++, but it is arguably safer and better practice to use 'std::string' objects from the C++ standard library. 'std::string' objects automatically allocate the amount of memory that they need to hold your strings from the heap, automatically free this memory when it is no longer needed, and implement a version of the '+' operator that does what you want (concatenation):

#include <string>

static const std::string Fruits[] = { "Mango", "Apple", "Pear", "Banana" };
static const std::string Prefix = "Chosen Fruit ";
std::string ChosenFruit = Prefix + Fruits[index];

The expression "ChosenFruit = Prefix + ..." means "call the version of the '+' operator which takes two 'std::string' objects as arguments". This '+' operator concatenates the two strings and returns a new 'std::string' object that contains the result.

Upvotes: 0

Remy Lebeau
Remy Lebeau

Reputation: 595782

You can't append a char/const char* to a const char[] (your string literal). But you can concatenate a std::string and a string literal, eg:

static const std::string FruitsList[] = { "Mango", "Apple", "Pear", "Bannana"};
indexFruit = 2;
std::string chosenFruitText = "Chosen Fruit" + FruitsList[indexFruit];
ImGui::ListBox(chosenFruitText.c_str(), &indexFruit, FruitsList, IM_ARRAYSIZE(FruitsList));

Otherwise, you would have to do something more like this instead:

static const char* FruitsList[] = { "Mango", "Apple", "Pear", "Bannana"};
indexFruit = 2;
char chosenFruitText[22] = "Chosen Fruit: ";
strcpy(chosenFruitText+14, FruitsList[indexFruit]);
ImGui::ListBox(chosenFruitText, &indexFruit, FruitsList, IM_ARRAYSIZE(FruitsList));

Upvotes: 1

Related Questions