how to return a char array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ger

    how to return a char array

    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




  • Nick Hounsome

    #2
    Re: how to return a char array


    "Ger" <g.inberg@geens pam.student.utw ente.nl> wrote in message
    news:bv3hfo$3se $1@ares.cs.utwe nte.nl...[color=blue]
    > Hi,
    >
    > I have been trying to return a data packet (as a char array) at the end of[/color]
    a[color=blue]
    > function but that seems to be impossible...
    > I would like to use this char array in a different class..anyone some[/color]
    ideas?[color=blue]
    >
    > greetz,
    > Ger
    >[/color]

    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)


    Comment

    • osmium

      #3
      Re: how to return a char array

      Ger writes:
      [color=blue]
      > I have been trying to return a data packet (as a char array) at the end of[/color]
      a[color=blue]
      > function but that seems to be impossible...
      > I would like to use this char array in a different class..anyone some[/color]
      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.


      Comment

      • Mike Wahler

        #4
        Re: how to return a char array

        "Ger" <g.inberg@geens pam.student.utw ente.nl> wrote in message
        news:bv3hfo$3se $1@ares.cs.utwe nte.nl...[color=blue]
        > Hi,
        >
        > I have been trying to return a data packet (as a char array) at the end of[/color]
        a[color=blue]
        > function but that seems to be impossible...[/color]

        Right. Passing and returning arrays to/from functions is
        not allowed. Pass a pointer instead.
        [color=blue]
        > I would like to use this char array in a different class..anyone some[/color]
        ideas?

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


        int main()
        {
        char *array = new char[DESIRED_SIZE];
        get_packet(arra y);
        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


        Comment

        • Kevin Goodsell

          #5
          Re: how to return a char array

          osmium wrote:
          [color=blue]
          > Ger writes:
          >
          >[color=green]
          >>I have been trying to return a data packet (as a char array) at the end of[/color]
          >
          > a
          >[color=green]
          >>function but that seems to be impossible...
          >>I would like to use this char array in a different class..anyone some[/color]
          >
          > 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.
          >
          >[/color]

          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.

          Comment

          • Bryan Bullard

            #6
            Re: how to return a char array


            "Ger" <g.inberg@geens pam.student.utw ente.nl> wrote in message
            news:bv3hfo$3se $1@ares.cs.utwe nte.nl...

            try to avoid using byte arrays to represent strings in c++. std::string is
            safer and very flexible.
            [color=blue]
            > Hi,
            >
            > I have been trying to return a data packet (as a char array) at the end of[/color]
            a[color=blue]
            > function but that seems to be impossible...
            > I would like to use this char array in a different class..anyone some[/color]
            ideas?[color=blue]
            >
            > greetz,
            > Ger
            >
            >
            >
            >[/color]


            Comment

            • NKOBAYE027

              #7
              Re: how to return a char array

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

              "Ger" <g.inberg@geens pam.student.utw ente.nl> wrote in message
              news:bv3hfo$3se $1@ares.cs.utwe nte.nl...[color=blue]
              > Hi,
              >
              > I have been trying to return a data packet (as a char array) at the end of[/color]
              a[color=blue]
              > function but that seems to be impossible...
              > I would like to use this char array in a different class..anyone some[/color]
              ideas?[color=blue]
              >
              > greetz,
              > Ger
              >
              >
              >
              >[/color]


              Comment

              • Jeff Schwab

                #8
                Re: how to return a char array

                [color=blue]
                > "Ger" <g.inberg@geens pam.student.utw ente.nl> wrote in message
                > news:bv3hfo$3se $1@ares.cs.utwe nte.nl...
                >[color=green]
                >>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[/color]
                >
                > ideas?
                >[color=green]
                >>greetz,
                >>Ger[/color][/color]

                NKOBAYE027 wrote:[color=blue]
                > return a reference to a std::vector<cha r> instead - in C++, unless you
                > absolutely have no choice, you should never use arrays. arrays
                > are evil - use standard containers instead.[/color]

                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.

                Comment

                • Attila Feher

                  #9
                  Re: how to return a char array

                  Jeff Schwab wrote:[color=blue]
                  > NKOBAYE027 wrote:[color=green]
                  > > return a reference to a std::vector<cha r> instead - in C++, unless[/color]
                  > you > absolutely have no choice, you should never use arrays. arrays[color=green]
                  > > are evil - use standard containers instead.[/color]
                  >
                  > 1) That's a load of baloney.[/color]

                  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. :-)
                  [color=blue]
                  > 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.[/color]

                  It's fun! :-) (My program works in debug mode, but not in release. Please
                  help! ;)
                  [color=blue]
                  > 3) Please don't top-post.[/color]

                  Agreed!

                  --
                  Attila aka WW


                  Comment

                  • Jeff Schwab

                    #10
                    Re: how to return a char array

                    Attila Feher wrote:[color=blue]
                    > Jeff Schwab wrote:
                    >[color=green]
                    >>NKOBAYE027 wrote:[color=darkred]
                    >> > return a reference to a std::vector<cha r> instead - in C++, unless[/color]
                    >> you > absolutely have no choice, you should never use arrays. arrays[color=darkred]
                    >> > are evil - use standard containers instead.[/color]
                    >>
                    >>1) That's a load of baloney.[/color]
                    >
                    >
                    > 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. :-)[/color]

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

                    [color=blue][color=green]
                    >>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.[/color]
                    >
                    >
                    > It's fun! :-) (My program works in debug mode, but not in release. Please
                    > help! ;)[/color]

                    Livin' on the edge, baby! "I like bungee jumping and volcano luging,
                    and in my spare time I dereference null..."
                    [color=blue][color=green]
                    >>3) Please don't top-post.[/color]
                    >
                    >
                    > Agreed!
                    >
                    > --
                    > Attila aka WW
                    >
                    >[/color]

                    Comment

                    • NKOBAYE027

                      #11
                      Re: how to return a char array

                      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. :o) Sorry to have offended any former
                      C programmers.

                      regards,
                      Lup
                      "NKOBAYE027 " <NKOBAYE027@Rog ers.Com> wrote in message
                      news:5tqRb.2141 7$9Ce1.7832@new s04.bloor.is.ne t.cable.rogers. com...[color=blue]
                      > return a reference to a std::vector<cha r> instead - in C++, unless you
                      > absolutely have no choice, you should never use arrays. arrays are evil -
                      > use standard containers instead.
                      >
                      > "Ger" <g.inberg@geens pam.student.utw ente.nl> wrote in message
                      > news:bv3hfo$3se $1@ares.cs.utwe nte.nl...[color=green]
                      > > Hi,
                      > >
                      > > I have been trying to return a data packet (as a char array) at the end[/color][/color]
                      of[color=blue]
                      > a[color=green]
                      > > function but that seems to be impossible...
                      > > I would like to use this char array in a different class..anyone some[/color]
                      > ideas?[color=green]
                      > >
                      > > greetz,
                      > > Ger
                      > >
                      > >
                      > >
                      > >[/color]
                      >
                      >[/color]


                      Comment

                      • Attila Feher

                        #12
                        Re: how to return a char array

                        Jeff Schwab wrote:[color=blue][color=green]
                        >> 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. :-)[/color]
                        >
                        > You're right: Vectors are, indeed, more appropriate as a learning tool
                        > (and for most production purposes).[/color]

                        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.
                        [color=blue]
                        > I'm saddened by the "arrays are the devil" nonsense, though. :([/color]

                        It is "the teaching". It is easier to scare people away from something (cf.
                        religions) than teach them to be responsible. ;-)[color=blue][color=green]
                        >> It's fun! :-) (My program works in debug mode, but not in release.
                        >> Please help! ;)[/color]
                        >
                        > Livin' on the edge, baby! "I like bungee jumping and volcano luging,
                        > and in my spare time I dereference null..."[/color]

                        ROFL!

                        --
                        Attila aka WW


                        Comment

                        Working...