Skip to main content
gamedev.net
🔒 Locked

converting char[] to string

Started by dk32321 2 replies 350+ views
Original Post
dk32321
dk32321
is it possible to convert char var[23]; to a string varaible. If somebody could help me i would appreciate it.
Zakwayda
Zakwayda
Quote:
Original post by dk32321
is it possible to convert char var[23]; to a string varaible.
std::string s = var;
Or:
std::string s(var);
Just make sure the input is null-terminated.
Simian Man
Simian Man
Just use the = operator:
char var[23];// fill the arraystd::string string_var = var;


I think it will have to be NULL terminated for this to work.
Fruny
Fruny
Quote:
Original post by Simian Man
I think it will have to be NULL terminated for this to work.


Correct. If it isn't, you can do string str(var, var+23); or whatever the length of the string/array is supposed to be.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.