Function Return Char[]

D

david

Hi All,

I just want to write a function return char array,but I cant. I am not
good at C++ 's concept since I used to use Java before. Would you please
help me to fix that ? Thanks..

Best Regards,
David
 
K

Kevin Goodsell

david said:
Hi All,

I just want to write a function return char array,but I cant. I am not
good at C++ 's concept since I used to use Java before. Would you please
help me to fix that ? Thanks..

Arrays are not "First class citizens" in C++. They cannot be passed to a
function or returned from a function (not directly, anyway). You have a
few options.

1. You can return a pointer to an array.
2. You can return some kind of class object containing an array.
3. You can return a container, such as std::vector.

I'd recommend option 3.

Option 1 has a few gotchas, such as 1) you'd damn well better not return
a pointer to a local automatic array, 2) if you return a static array
you might have problems because a second call to the function will
modify the result from an earlier call, and 3) if you return a dynamic
array, the caller has to be sure to free that array.

Option 2 is not very elegant or flexible.

-Kevin
 
D

david

Thanks a lot.
Kevin Goodsell said:
Arrays are not "First class citizens" in C++. They cannot be passed to a
function or returned from a function (not directly, anyway). You have a
few options.

1. You can return a pointer to an array.
2. You can return some kind of class object containing an array.
3. You can return a container, such as std::vector.

I'd recommend option 3.

Option 1 has a few gotchas, such as 1) you'd damn well better not return
a pointer to a local automatic array, 2) if you return a static array
you might have problems because a second call to the function will
modify the result from an earlier call, and 3) if you return a dynamic
array, the caller has to be sure to free that array.

Option 2 is not very elegant or flexible.

-Kevin
 
A

A

Either return a std::string (preferable) or a pointer to an array of char
that has been either statically or dynamically allocated.

#include <string>

class Foo{
public:
string func1();
char[] func2();
}
...
 
A

Artie Gold

A said:
not

please


Either return a std::string (preferable) or a pointer to an array of char
that has been either statically or dynamically allocated.


#include <string>

class Foo{
public:
string func1(); OK.

char[] func2();
No, you can't do that. Please reread my quoted response.
Cheers,
--ag
 
J

John L Fjellstad

david said:
I just want to write a function return char array,but I cant. I am not
good at C++ 's concept since I used to use Java before. Would you please
help me to fix that ? Thanks..

Even in Java, you can't return an array, can you? You can return a
std::vector, that corresponds to Vector in Java.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top