Sign in to follow this  

Return string from function?

This topic is 4609 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, so I have a function (obviously) that gets a registry value, and stores it to a string value. Is there any way I can return this value from my function?

Share this post


Link to post
Share on other sites
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
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

This topic is 4609 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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