how to return a char array

Discussion in 'C++' started by Ger, Jan 26, 2004.

  1. Ger

    Ger Guest

    Hi,

    I have been trying to return a data packet (as a char array) at the end of a
    function but that seems to be impossible...
    I would like to use this char array in a different class..anyone some ideas?

    greetz,
    Ger
    Ger, Jan 26, 2004
    #1
    1. Advertising

  2. "Ger" <> wrote in message
    news:bv3hfo$3se$...
    > Hi,
    >
    > I have been trying to return a data packet (as a char array) at the end of

    a
    > function but that seems to be impossible...
    > I would like to use this char array in a different class..anyone some

    ideas?
    >
    > greetz,
    > Ger
    >


    You can either:
    1 return a char* pointer to an array (better not be an automatic one).
    2 create a struct with an array in it and return one of those (almost
    certainly a bad idea)
    3. pass in a reference to a std::vector and fill it (pprobably the best bet
    without knowing more)
    Nick Hounsome, Jan 26, 2004
    #2
    1. Advertising

  3. Ger

    osmium Guest

    Ger writes:

    > I have been trying to return a data packet (as a char array) at the end of

    a
    > function but that seems to be impossible...
    > I would like to use this char array in a different class..anyone some

    ideas?

    Allocate space for the char array, using new, in some function *before*
    calling the 'data packet' function. Pass a pointer to this array around
    until its meaningful lifetime has expired. Then delete it with delete. The
    assignment and deletion do not have to be performed in the same function.
    osmium, Jan 26, 2004
    #3
  4. Ger

    Mike Wahler Guest

    "Ger" <> wrote in message
    news:bv3hfo$3se$...
    > Hi,
    >
    > I have been trying to return a data packet (as a char array) at the end of

    a
    > function but that seems to be impossible...


    Right. Passing and returning arrays to/from functions is
    not allowed. Pass a pointer instead.

    > I would like to use this char array in a different class..anyone some

    ideas?

    void get_packet(char *arg)
    {
    strcpy(arg, "data");
    some_function_that_gets_packet(arg);
    }


    int main()
    {
    char *array = new char[DESIRED_SIZE];
    get_packet(array);
    cout << array << '\n'; // outputs "data"
    }

    It's also a good idea to send a second argument as
    well: the size of the array, so you can protect
    against an overrun.

    An alternative to the above is to wrap the array in a
    class or struct, and return that. Or store the array
    contents in a standard container (e.g. vector) and return
    that, and if the caller needs the 'raw' array form, you
    can recreate it from the container data.

    -Mike
    Mike Wahler, Jan 26, 2004
    #4
  5. osmium wrote:

    > Ger writes:
    >
    >
    >>I have been trying to return a data packet (as a char array) at the end of

    >
    > a
    >
    >>function but that seems to be impossible...
    >>I would like to use this char array in a different class..anyone some

    >
    > ideas?
    >
    > Allocate space for the char array, using new, in some function *before*
    > calling the 'data packet' function. Pass a pointer to this array around
    > until its meaningful lifetime has expired. Then delete it with delete. The
    > assignment and deletion do not have to be performed in the same function.
    >
    >


    Better use new[] and delete[], not new and delete.

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
    Kevin Goodsell, Jan 26, 2004
    #5
  6. "Ger" <> wrote in message
    news:bv3hfo$3se$...

    try to avoid using byte arrays to represent strings in c++. std::string is
    safer and very flexible.

    > Hi,
    >
    > I have been trying to return a data packet (as a char array) at the end of

    a
    > function but that seems to be impossible...
    > I would like to use this char array in a different class..anyone some

    ideas?
    >
    > greetz,
    > Ger
    >
    >
    >
    >
    Bryan Bullard, Jan 26, 2004
    #6
  7. Ger

    NKOBAYE027 Guest

    return a reference to a std::vector<char> instead - in C++, unless you
    absolutely have no choice, you should never use arrays. arrays are evil -
    use standard containers instead.

    "Ger" <> wrote in message
    news:bv3hfo$3se$...
    > Hi,
    >
    > I have been trying to return a data packet (as a char array) at the end of

    a
    > function but that seems to be impossible...
    > I would like to use this char array in a different class..anyone some

    ideas?
    >
    > greetz,
    > Ger
    >
    >
    >
    >
    NKOBAYE027, Jan 27, 2004
    #7
  8. Ger

    Jeff Schwab Guest


    > "Ger" <> wrote in message
    > news:bv3hfo$3se$...
    >
    >>Hi,
    >>
    >>I have been trying to return a data packet (as a char array) at the end of
    >>a function but that seems to be impossible...
    >>I would like to use this char array in a different class..anyone some

    >
    > ideas?
    >
    >>greetz,
    >>Ger


    NKOBAYE027 wrote:
    > return a reference to a std::vector<char> instead - in C++, unless you
    > absolutely have no choice, you should never use arrays. arrays
    > are evil - use standard containers instead.


    1) That's a load of baloney.

    2) Returning a reference to a local vector doesn't
    solve the problem. The vector will be destroyed
    before the caller gets to use it.

    3) Please don't top-post.
    Jeff Schwab, Jan 27, 2004
    #8
  9. Ger

    Attila Feher Guest

    Jeff Schwab wrote:
    > NKOBAYE027 wrote:
    > > return a reference to a std::vector<char> instead - in C++, unless

    > you > absolutely have no choice, you should never use arrays. arrays
    > > are evil - use standard containers instead.

    >
    > 1) That's a load of baloney.


    Actually the "avoid arrays" is a good advice for beginners. You can do
    everything with a vector, and more safely. Of course this is no rule for
    production code. :)

    > 2) Returning a reference to a local vector doesn't
    > solve the problem. The vector will be destroyed
    > before the caller gets to use it.


    It's fun! :) (My program works in debug mode, but not in release. Please
    help! ;)

    > 3) Please don't top-post.


    Agreed!

    --
    Attila aka WW
    Attila Feher, Jan 27, 2004
    #9
  10. Ger

    Jeff Schwab Guest

    Attila Feher wrote:
    > Jeff Schwab wrote:
    >
    >>NKOBAYE027 wrote:
    >> > return a reference to a std::vector<char> instead - in C++, unless

    >> you > absolutely have no choice, you should never use arrays. arrays
    >> > are evil - use standard containers instead.

    >>
    >>1) That's a load of baloney.

    >
    >
    > Actually the "avoid arrays" is a good advice for beginners. You can do
    > everything with a vector, and more safely. Of course this is no rule for
    > production code. :)


    You're right: Vectors are, indeed, more appropriate as a learning tool
    (and for most production purposes). I'm saddened by the "arrays are the
    devil" nonsense, though. :(


    >>2) Returning a reference to a local vector doesn't
    >> solve the problem. The vector will be destroyed
    >> before the caller gets to use it.

    >
    >
    > It's fun! :) (My program works in debug mode, but not in release. Please
    > help! ;)


    Livin' on the edge, baby! "I like bungee jumping and volcano luging,
    and in my spare time I dereference null..."

    >>3) Please don't top-post.

    >
    >
    > Agreed!
    >
    > --
    > Attila aka WW
    >
    >
    Jeff Schwab, Jan 27, 2004
    #10
  11. Ger

    NKOBAYE027 Guest

    ooops forgive the return of reference faux pas...i was thinking the same as
    Nick's answer above - hand in a reference to that vector and then modify the
    referenced object within the function. shows that we shouldn't type before
    we think...

    as to the comment arrays are evil, it's a quote from M. Cline, who sits on
    the ANSI C++ standards committee, is his C++ FAQ Lite at
    http://www.parashift.com/c -faq-lite/index.html it's meant to be tongue in
    cheek and taken with a grain of salt. :eek:) Sorry to have offended any former
    C programmers.

    regards,
    Lup
    "NKOBAYE027" <> wrote in message
    news:5tqRb.21417$...
    > return a reference to a std::vector<char> instead - in C++, unless you
    > absolutely have no choice, you should never use arrays. arrays are evil -
    > use standard containers instead.
    >
    > "Ger" <> wrote in message
    > news:bv3hfo$3se$...
    > > Hi,
    > >
    > > I have been trying to return a data packet (as a char array) at the end

    of
    > a
    > > function but that seems to be impossible...
    > > I would like to use this char array in a different class..anyone some

    > ideas?
    > >
    > > greetz,
    > > Ger
    > >
    > >
    > >
    > >

    >
    >
    NKOBAYE027, Jan 27, 2004
    #11
  12. Ger

    Attila Feher Guest

    Jeff Schwab wrote:
    >> Actually the "avoid arrays" is a good advice for beginners. You can
    >> do everything with a vector, and more safely. Of course this is no
    >> rule for production code. :)

    >
    > You're right: Vectors are, indeed, more appropriate as a learning tool
    > (and for most production purposes).


    Well, in production they might happily hide errors as well (if you really do
    have a small upper limit). But I would say that one is still better off
    using the boost array wrapper than "naked" arrays.

    > I'm saddened by the "arrays are the devil" nonsense, though. :(


    It is "the teaching". It is easier to scare people away from something (cf.
    religions) than teach them to be responsible. ;-)
    >> It's fun! :) (My program works in debug mode, but not in release.
    >> Please help! ;)

    >
    > Livin' on the edge, baby! "I like bungee jumping and volcano luging,
    > and in my spare time I dereference null..."


    ROFL!

    --
    Attila aka WW
    Attila Feher, Jan 28, 2004
    #12
    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:
    538
  2. lovecreatesbeauty
    Replies:
    1
    Views:
    1,010
    Ian Collins
    May 9, 2006
  3. Replies:
    3
    Views:
    720
  4. davidb
    Replies:
    0
    Views:
    752
    davidb
    Sep 1, 2006
  5. davidb
    Replies:
    6
    Views:
    1,534
    Default User
    Sep 1, 2006
Loading...

Share This Page