How concatenate string type with char array type

How concatenate string type with pointer to char (char array) type

#include <iostream>
using namespace std;
//...

string m, s = "foo";
const char *t = "bar";

// m = s + t ? // illustration only

cout<<"all message = "<< m;
Last edited on
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <string>
using namespace std;

int main()
{
   string f = "foo";
   const char *b = "bar";
   string m = f + b;
   cout<<"all message = "<< m;
}
i did get solution 3 miniutes after post asking but just know tell it:

#include <iostream>
using namespace std;
//...

string m, s = "foo";
const char *t = "bar";

m = s + string(t);

cout<<"all message = "<< m;
@abdulbadii,
You do NOT need to explicitly convert your const char* to a std::string to do that addition. Check out the reference on this site for the overloaded string + operators. Or just run my code sample in cpp.sh

You are creating a problem where one didn't exist.
// m = s + t ? // illustration only


Why "illustration only"? This does work.
Topic archived. No new replies allowed.