Need funcion to return char[]?

Started by
20 comments, last by King Mir 11 years, 2 months ago

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

Advertisement
With std::string it works pretty much like it does with an int.

std::string foo(std::string & temp) {
  temp = "123";
  return "456";
}

std::string A, B;
A = foo(B);

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

Thanks SiCrane, I think I can work with that...

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...

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;
}

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.

cool... thanks....

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.

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.

o3o

This topic is closed to new replies.

Advertisement