Jump to content
  • Advertisement
Sign in to follow this  
Vanz

Need funcion to return char[]?

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

I have some old code I'm trying to work with and I need a function to return a name, so for example:

 

[source]

int cApp::ReturnInt(int &Temp)

{

Temp=5;

return 6;

}

 

... later code...

int A,B;

A=ReturnInt(B);

[/source]

 

Returns A=5 and B=6 

 

Now I need to do this with a char[] name, like this somehow:

 

[source]

char[20] cApp::ReturnInt(char[30] &Temp)

{

strcpy_s(Temp, "123");        //Add 123 into Temp
return "456";

}

 

... later code...

char[20] A,B;

A=ReturnInt(B);

[/source]

 

Which doesn't work of course, but I hope you get the gist of what I'm trying to do... I don't need it to return the name both ways (using return and &) just any way...

 

It's too hard for me to update all the code to string but if you have the time I'd also like to see how it's done with string...

 

Hope this makes sense...

 

Thanks,

 

Vanz

Share this post


Link to post
Share on other sites
Advertisement

As SiCrane is hinting at, with C++ always prefer std::string unless you have a real reason not to.

 

ya I do, like I said above I'm working with some really old code and it'll take too long to convert it all...

Share this post


Link to post
Share on other sites
You can't return arrays. Instead, you can return a pointer to char. You will have to pass in the return value too.
char * example(char Temp[], char * buf)
{
    //do work to temp, store result to buf
    return buf;
}

Share this post


Link to post
Share on other sites

As SiCrane is hinting at, with C++ always prefer std::string unless you have a real reason not to.

 
ya I do, like I said above I'm working with some really old code and it'll take too long to convert it all...

 

It'll take too long on the front end, but over the lifetime of the project, you'll probably waste more time fighting the chars.
If you have deadlines though, then there may not be enough time to meet the deadlines and refactor the project. Still, if you could schedule a time (three days or whatever you estimate it'll take) in the near future to refactor that aspect of the project, I think you'll feel alot better about it - and the code will certainly benefit from it.

But that's just my opinion!

Even if you can't use std::strings everywhere, you could still use them as a container-type for char arrays.

std::string cApp::ReturnString(char[30] &Temp)
{
	std::string copy(temp, 30);
	//...
	return copy;
}
 
... later code...
std::string B;
std::string A = DoSomething(B[0], B.size());

(Assuming DoSomething() already exists as part of the old code-base and can't be changed. Otherwise, it'd be much better off just taking a std::string)
 
But everything new you write would be much better off as std::string.
If you absolutely have-to-have-to-have-to, you could do something like this:

struct CharArrayStruct30
{
    char data[30]; //Horrible. *shudders*
};

 
But even this would be a step up:

struct RawStringStruct
{
    char *data;
    size_t size;
};

 
But std::strings constructors and functions are compatible with C-style raw strings, so you can migrate your codebase away slowly if you like.

Share this post


Link to post
Share on other sites

I mentioned this in another thread, and this is a common tripping point, but an array isn't really a variable, its just a trick to look like one.  At the end of the day, an array is just syntactic sugar over a continuous block of memory.  

Share this post


Link to post
Share on other sites
You could make a template class to do something like

Array<char,30> chars;
chars.array[3]='a';

Which should work as a return value/patameter, and you dont need a new struct for different array sizes.

Unless you can just use string of course.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!