Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a const char * to std::string [duplicate]

Tags:

I've got a const char * returned from a processing function, and I'd like to convert/assign it to an instance of std::string for further manipulation. This seems like it should be straight-forward, but I've not been able to find any documentation showing how it should be done. Obviously, I'm missing something. Insights appreciated.

like image 830
Justin Fletcher Avatar asked Jun 09 '14 19:06

Justin Fletcher


People also ask

Can you convert char * to string?

We can convert a char to a string object in java by using the Character. toString() method.

Can const char * Be Changed?

const char* const says that the pointer can point to a constant char and value of int pointed by this pointer cannot be changed. And we cannot change the value of pointer as well it is now constant and it cannot point to another constant char.

Does std :: string make a copy?

std::string::copyCopies a substring of the current value of the string object into the array pointed by s. This substring contains the len characters that start at position pos. The function does not append a null character at the end of the copied content.


1 Answers

std::stringhas a constructor fromconst char *.This means that it is legal to write:

const char* str="hello"; std::string s = str; 
like image 110
Ivaylo Strandjev Avatar answered Sep 21 '22 15:09

Ivaylo Strandjev