Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

From char to string

Status
Not open for further replies.

grande

Programmer
Feb 14, 2005
657
CA
Is there an easy way to change a C-type char to a C++-type string.

I currently have a name being passed through a function (char name[50]) into a string. It compiles fine, but the string in the function is empty. Weird, no?

By the way, if it matters, the function is actually a method of a class.

-------------------------
Just call me Captain Awesome.
 
Yes, very weird. You should post relevant code. A simple assignment should work assuming name is not null and is null terminated.
 
Yeah, I figured it out. You can't pass a char into a string through a function, but if you just say string = char, it's not a problem.

Thanks for responding!

-------------------------
Just call me Captain Awesome.
 
Actually, you cannot say string = char (a char is a single character), but you can say string = char* or char[]. And you can pass a char* or char[] into a function that takes a string because the string class has a constructor that takes a char*. So whatever was wrong had nothing to do with passing a a C style string to a function expecting a C++ string.

Glad you got it working, though.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top