Function Return Char[]

Discussion in 'C++' started by david, Aug 29, 2003.

  1. david

    david Guest

    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
    david, Aug 29, 2003
    #1
    1. Advertising

  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.
    Kevin Goodsell, Aug 29, 2003
    #2
    1. Advertising

  3. david

    david Guest

    Thanks a lot.
    "Kevin Goodsell" <> ?????
    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.
    >
    david, Aug 29, 2003
    #3
  4. david

    A Guest


    > > 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();
    }
    ...
    A, Aug 30, 2003
    #4
  5. david

    Artie Gold Guest

    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
    Artie Gold, Aug 30, 2003
    #5
  6. 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?
    John L Fjellstad, Aug 30, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. wwj
    Replies:
    7
    Views:
    537
  2. wwj
    Replies:
    24
    Views:
    2,475
    Mike Wahler
    Nov 7, 2003
  3. Ben Pfaff
    Replies:
    5
    Views:
    458
    Tristan Miller
    Jan 17, 2004
  4. Replies:
    8
    Views:
    524
    Bryan Donlan
    Aug 26, 2005
  5. lovecreatesbeauty
    Replies:
    1
    Views:
    1,006
    Ian Collins
    May 9, 2006
Loading...

Share This Page