Sign in to follow this  
PyroBlizzard

Return string from function?

Recommended Posts

Agony    3452
I'm assuming you're using C++. If not, let us know which language you are using. If C++, though, the preferred method would be to use std::string, found in <string>. It doesn't have any of the problems of the C-style string (char*) would have in this situation. The character data will actually get copied automatically for you, and all dynamically allocated memory will be freed without you having to worry about it.

#include <string>

std::string ReturnAString()
{
std::string s;
//stuff
return s;
}

If you ever need to read from a std::string as though it were a character array, use the function s.c_str(). This will return a const char*. If you need to write into it as though it's a character array, then you'll likely just have to create a temporary character array, read the data into that, and then set the string variable equal to the character array; all the data will get copied into the string object.

Share this post


Link to post
Share on other sites
PyroBlizzard    122
Thanks for the help, but I figured it out right before I saw that. I am using managed C++, so for the reference of anybody else who might have the problem, I had to do such

Quote:
System::String^ functionName()
{
//do stuff
return stringName;
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this