In order to convert char*
and char[]
to string in c++
, you can use multiple ways like std::string()
method or c_str()
method. Check the code –
1 – char[] to string using std::string()
char name[] = {"Ironman"}; std::string strName = std::string(name);
2 – char* to string
char *name = "Captain America"; std::string strName = std::string(name);
or
const char *name = "Thor"; std::string str(name);