How do i convert a character array to string.

V

Venkat

Hi All,

Can someone tell me how do we convert a character array to string.
I know the other way.

For eg:-
String strvar = "hello";
printf("%s",strvar.c_str());

regards,
Venkat
 
S

Srini

Hi All,
Can someone tell me how do we convert a character array to string.
I know the other way.

For eg:-
String strvar = "hello";
printf("%s",strvar.c_str());

I hope you meant, string(lowercase 's'). And what are you doing in the
first statement? Converting a array of chars to a string object.

char arr[] = "Hello";
std::string str(arr);
regards,
Venkat

Srini
 
D

Default User

Venkat said:
Hi All,

Can someone tell me how do we convert a character array to string.
I know the other way.

For eg:-
String strvar = "hello";
printf("%s",strvar.c_str());

I assume the "String" with the uppercase S is a typo, and you really
mean std::string. std::string has both a constructor and an assignment
operator that char* arguments.


std::string s("hello");

std::string s;

s = "hello";



You need to get a decent reference book that covers these things. This
is basic stuff.




Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top