471,585 Members | 1,165 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,585 software developers and data experts.

Function Return Char[]

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
Jul 19 '05 #1
5 15728
Thanks a lot.
"Kevin Goodsell" <us*********************@neverbox.com> ?????
news:3f4edce1@shknews01...
david wrote:
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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #2
david wrote:
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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #3
A

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

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();
}
...


Jul 19 '05 #4
A wrote:
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..


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
--
Artie Gold -- Austin, Texas

Jul 19 '05 #5
david wrote:
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.

--
John L. Fjellstad

A: Top posting!
Q: What is the most irritating thing on Usenet?
Jul 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by muser | last post: by
1 post views Thread by Charlie | last post: by
6 posts views Thread by aurgathor | last post: by
35 posts views Thread by michael.casey | last post: by
2 posts views Thread by Fernando Barsoba | last post: by
9 posts views Thread by cmk128 | last post: by
4 posts views Thread by default | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.