Return string from function?
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?
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.
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.
#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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement