Jump to content
  • Advertisement

Archived

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

drsixstring

Functions that return a string

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

Advertisement
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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!