Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conversion of char * to string

Tags:

c++

c

Does anyone have an idea how to convert char* to string. Actually, I have a function which returns value as char* and now i need to store/copy std::string. I have tried something like

char *sResult = (char*)malloc(1024); std:string line; line= line+ sResult

Thanks and regards, Sam

like image 703
samprat Avatar asked Dec 01 '25 08:12

samprat


2 Answers

There is an implicit conversion from const char* to std::string(via nonexplicit constructor). So the following will all work (suppose ch is const char* or char*)

std::string s (ch);
std::string s = ch;
s = ch;
f(ch); //where f is declared to take std::string
etc
like image 54
Armen Tsirunyan Avatar answered Dec 03 '25 20:12

Armen Tsirunyan


How about

std::string line(szResult);
like image 34
kellogs Avatar answered Dec 03 '25 22:12

kellogs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!