Archived

This topic is now archived and is closed to further replies.

Functions that return a string

This topic is 5780 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

Guest Anonymous Poster
quote:
Original post by drsixstring

This doesn''t work:


    
char[5] GetString()
{
char str[5] = "hello";
return str;
}


How would I write a function that would return a string?

    
char* GetString()
{
char str* = strdup("hello");
return str;
}


Share this post


Link to post
Share on other sites
there isnt any way to maintain the integrity of an arrays size, at compile time, without actually stating it.

so you may need to pass a byref length argument.

void getArray(char str[], int &length);

often, a string will be NULL terminated, meaning the last character in it is 0.

void getArray(char zStr[]);

if you want to return an array, then you use char*
char* getArray();

Share this post


Link to post
Share on other sites