Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Function Return Char[]

Reply
Thread Tools

Function Return Char[]

 
 
david
Guest
Posts: n/a
 
      08-29-2003
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


 
Reply With Quote
 
 
 
 
Kevin Goodsell
Guest
Posts: n/a
 
      08-29-2003
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.

 
Reply With Quote
 
 
 
 
david
Guest
Posts: n/a
 
      08-29-2003
Thanks a lot.
"Kevin Goodsell" <(E-Mail Removed)> ?????
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.
>



 
Reply With Quote
 
A
Guest
Posts: n/a
 
      08-29-2003


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




 
Reply With Quote
 
Artie Gold
Guest
Posts: n/a
 
      08-30-2003
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

 
Reply With Quote
 
John L Fjellstad
Guest
Posts: n/a
 
      08-30-2003
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?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
function doesn't return simply skip the return line hirsh.dan@gmail.com C++ 4 07-22-2008 09:55 AM
Strange Perl line : Return the result of a function to a function AlexHWGUY Perl Misc 5 10-24-2006 09:27 PM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
getting return value from function without return statement. Seong-Kook Shin C Programming 1 06-18-2004 08:19 AM



Advertisments