I found an c++ example in a book that includes functions that return char arrays. I have omitted the irrelevant parts:
class CArgNode
{
private:
char szCommand[255], szParameter[255];
//...
public:
//...
char * GetCommand();
};
char* CArgNode::GetCommand()
{
return (char*)this->szCommand;
}
A string is strcpy'd to szCommand in the constructor.
One thing I notice that is different for my function is that the char array that it returns is a class variable.
PaulS:
You could call malloc() to allocate space on the heap, instead, and return a pointer to that space. You need to be sure to free() it when done with it.
How would I do that? Do I have to use free() outside of the function?
michael_x:
If you need 3 texts, each max 50 characters wide, and you have the RAM space available, 3 char arrays definitely is the way to go.
What if the length of the strings can be anywhere from 4 to 50 characters?