I have a problem when trying to add a string to a char array, the console gives me a code error
This is my code:
std::string text1 = "hello";
std::string text2 = "world";
const char *texto[] = {
"Texto1: " std::text1,
"Texto2: " std::text2
}
The error it shows me is this expected '}'
:to match '}'
I do it in the following way:
strcpy()
Used to copy the string string2 into string1 . Returns the value of string1.strcat()
Allows one memory block to be added to another.The declarations of these functions are found in the header file
string.h
. Therefore, to be able to use this function in a program, you have to write:I hope it is useful to you.
These errors appear to you because the construction or is correct for two reasons:
std::text1
does not exist . You have a variabletext1
... but it is not declared in the namespacestd
.char*
does not have a constructor that collects a string of typechar
and astd::string
. If you want to do that you have to program your a function that does that operation. As you will see later, the operation is not trivial at all.char
andtext1
... the compiler is hardly going to know what you mean to do with it (you want to concatenate it, but the compiler isn't second-guessing).The simplest is to use an array of
string
:Although for this it is preferable to use
std::array
:However, if you have to forcefully use an array of
char
, then you will have to copy the text by hand, since the content of the text is only known at runtime. One possibility is to use lambdas (C++11):What the lambda does is generate the final result and copy it into a pointer of type
char
. Of course then you have to remember to clean the dynamic memory:If you can't (or won't) use lambdas, you can easily replace it with a standalone function:
The weird thing is that the compiler hasn't imploded and swept away your neighborhood. The code you've written doesn't make any sense in C++.
The way to initialize an array 1 of
char
is by writing a text literal after the assignment operator:The compiler will take care of calculating the size of array 1 including the string terminator , as you can see the curly braces are optional, but you could have used them:
You have written in your code a string literal, followed by a symbol that does not exist:
There is no way to directly do what you want, to begin with arrays 1 need to know their size at compile time unless they are created dynamically sized:
Once created with the required size, you can make the copy :
Don't forget to free the memory when you no longer use it:
Proposal.
Honestly, you are complicating your life, forget formation 1 and use another
std::string
:You complicate things more than necessary, String has a function called c_str , which returns a constant char pointer to the string
const char *
, your code could perfectly be the following:And your error is because a semicolon is missing at the end of the array declaration.
EDITED 1
I want to clarify something because I was very short, the way I put it to you, it works, it does specifically what you wanted, period, but you must take something into account, the c_str function returns a pointer to the current data array, if after the code that I ask you, were you to modify one of your two
string
, the matrixtexto[]
would be pointing to nothing, because a copy was not made, it was passed to the pointer , to avoid this, it is necessary to give memory to the matrix and make a copy of Thestring
, this code works:As you will notice, I have to give dynamic memory to the matrix, to make the copy, there you would already have a copy, now, I come back and say, you must have some reason to want to use a matrix
const char *
that occurs to me a lot, but if not, why ?, if you don't have a reason, usestd::vector<std::string>
the header<vector>
and you avoid many problems.