Archived

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

drsixstring

Functions that return a string

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
If you''re not adverse to c++ and the stl:


#include <string>
using std::string;

string GetString()
{
string str="hello";
return str;
}

Share this post


Link to post
Share on other sites
Thanks guys, that helps a lot. Soon after I posted I realized I would need to use a pointer. But Sjelkjd''s info on the string class in C++ will be something I''ll have to check out.

Share this post


Link to post
Share on other sites