reference to array?

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

    #1

    reference to array?

    Hope you know what I mean...


    void Work(int& r[4])
    {
    r[0]=0;
    }

    int main(int, char**)
    {
    int a[4]
    Work(a);
    }

    --
    -Gernot
    int main(int argc, char** argv) {printf
    ("%silto%c%cf%c gl%ssic%ccom%c" , "ma", 58, 'g', 64, "ba", 46, 10);}

    _______________ _______________ __________
    Looking for a good game? Do it yourself!
    GLBasic - you can do
    GLBasic is a programming language that supports multiple platforms like e.g. iPhone



  • John Harrison

    #2
    Re: reference to array?


    "Gernot Frisch" <Me@Privacy.net > wrote in message
    news:2sviarF1pj 0u6U1@uni-berlin.de...[color=blue]
    > Hope you know what I mean...
    >
    >
    > void Work(int& r[4])[/color]

    void Work(int (&r)[4])

    Reference to array, not array of references (which is not allowed).

    john


    Comment

    • Ron Natalie

      #3
      Re: reference to array?

      Gernot Frisch wrote:[color=blue]
      > Hope you know what I mean...
      >
      >
      > void Work(int& r[4])[/color]

      You've declared a four element array of references
      rather than a refernce to array of 4 ints (which would
      be int (&r)[4].

      However, you don't even need that. C++ inherits the
      braindamaged array passing behavior from C. A silent
      conversion of the function type to pointer-to-first element
      occurs.

      Your code will work the way you expect if you define the
      function as:
      void Work(int r[4]) {

      Arrays are not passed by value as would be consistant with every other
      type in the language. The array r is the same as the one in the caller
      here.

      Comment

      • JKop

        #4
        Re: reference to array?

        Gernot Frisch posted:
        [color=blue]
        > Hope you know what I mean...
        >
        >
        > void Work(int& r[4])
        > {
        > r[0]=0;
        > }
        >
        > int main(int, char**)
        > {
        > int a[4]
        > Work(a);
        > }
        >[/color]

        Here's some functions that'll alter an array of 4 elements:

        void Monkey(int* const p_blah) throw()
        {
        p_blah[0] = 67;
        p_blah[1] = -54;
        p_blah[2] = 76;
        p_blah[3] = 47;
        }

        void Ape(int blah[4]) throw() //Looks like it's passing by value...
        {
        blah[0] = 67;
        blah[1] = -54;
        blah[2] = 76;
        blah[3] = 47;
        }


        void Cow(int (&blah)[4]) throw()
        {
        blah[0] = 67;
        blah[1] = -54;
        blah[2] = 76;
        blah[3] = 47;
        }


        -JKop

        Comment

        • Gernot Frisch

          #5
          Re: reference to array?

          > Your code will work the way you expect if you define the[color=blue]
          > function as:
          > void Work(int r[4]) {[/color]

          How can I happen to be so silly to have forgotten that... Thank you
          very much.


          Comment

          • DaKoadMunky

            #6
            Re: reference to array?

            >Your code will work the way you expect if you define the[color=blue]
            >function as:
            > void Work(int r[4]) {[/color]

            But that would allow passing the address of any int to the function be it a
            single element or an array of any size.

            Maybe the function is hardcoded to work with arrays of a specific size.

            It seems the reference syntax would prevent this kind of usage...

            void func(int (&r)[3])
            {
            }

            int main()
            {
            int *x;
            func(x); //Sorry

            int y[2];
            func(y); //Nope

            return 0;
            }

            Do you see any value in the "reference to array" syntax?

            I have never needed it, but am curious to peoples opinions.





            Comment

            • Cy Edmunds

              #7
              Re: reference to array?

              "JKop" <NULL@NULL.NULL > wrote in message
              news:64xad.3316 5$Z14.12820@new s.indigo.ie...[color=blue]
              > Gernot Frisch posted:
              >[color=green]
              > > Hope you know what I mean...
              > >
              > >
              > > void Work(int& r[4])
              > > {
              > > r[0]=0;
              > > }
              > >
              > > int main(int, char**)
              > > {
              > > int a[4]
              > > Work(a);
              > > }
              > >[/color]
              >
              > Here's some functions that'll alter an array of 4 elements:
              >
              > void Monkey(int* const p_blah) throw()
              > {
              > p_blah[0] = 67;
              > p_blah[1] = -54;
              > p_blah[2] = 76;
              > p_blah[3] = 47;
              > }
              >[/color]

              I think const is a little misleading here. The function gets a copy of the
              pointer anyway so the caller wouldn't care if the function changed its
              value. Also, I see you have discovered throw specifications. Unfortunately
              they don't do what you might think (for instance prevent the function from
              throwing) and I think most experienced programmers avoid them.

              [snip]

              --
              Cy
              http://home.rochester.rr.com/cyhome/


              Comment

              • JKop

                #8
                Re: reference to array?

                [color=blue][color=green]
                >> void Monkey(int* const p_blah) throw()
                >> {
                >> p_blah[0] = 67;
                >> p_blah[1] = -54;
                >> p_blah[2] = 76;
                >> p_blah[3] = 47; }
                >>[/color]
                >
                > I think const is a little misleading here.[/color]

                I disagree. It's crystal clear to me that "p_blah" is a local object of type
                "int*" and that it's const. Sure, it's not needed, but I use "const"
                wherever possible and it allows for more obvious optimization.
                [color=blue]
                > The function gets a copy of
                > the pointer anyway so the caller wouldn't care if the function changed
                > its value.[/color]

                Agreed.
                [color=blue]
                > Also, I see you have discovered throw specifications.[/color]

                Indeed.
                [color=blue]
                > Unfortunately they don't do what you might think (for instance prevent
                > the function from throwing) and I think most experienced programmers
                > avoid them.[/color]

                I know exactly how it works. If this particular function threw an exception,
                then the function "std::unexpecte d" would be called.

                The reason I stick it in is that I *know* that my function won't ever throw
                an exception, and as with "const", I'll use it wherever possible.

                I wouldn't though for instance do the following:

                void blah()
                {
                string k("salj");
                }


                Because I don't know the insides of that type and it may very well throw an
                exception I'm unaware of.


                -JKop


                -JKop

                Comment

                • JKop

                  #9
                  Re: reference to array?

                  [color=blue]
                  > void blah()
                  > {
                  > string k("salj");
                  > }[/color]


                  Should've been:


                  void blah() throw()
                  {

                  string k("salj");
                  }


                  -JKop

                  Comment

                  • Richard Herring

                    #10
                    Re: reference to array?

                    In message <c_Mad.33221$Z1 4.12927@news.in digo.ie>, JKop <NULL@NULL.NULL >
                    writes[color=blue]
                    >[color=green]
                    >> void blah()
                    >> {
                    >> string k("salj");
                    >> }[/color]
                    >
                    >
                    >Should've been:
                    >
                    >
                    >void blah() throw()
                    >{
                    >
                    > string k("salj");
                    >}
                    >[/color]

                    Shouldn't.

                    What do you think happens if the string constructor can't allocate
                    enough memory?

                    --
                    Richard Herring

                    Comment

                    • JKop

                      #11
                      Re: reference to array?

                      Richard Herring posted:
                      [color=blue]
                      > In message <c_Mad.33221$Z1 4.12927@news.in digo.ie>, JKop <NULL@NULL.NULL >
                      > writes[color=green]
                      >>[color=darkred]
                      >>> void blah()
                      >>> {
                      >>> string k("salj"); }[/color]
                      >>
                      >>
                      >>Should've been:
                      >>
                      >>
                      >>void blah() throw()
                      >>{
                      >>
                      >> string k("salj"); }
                      >>[/color]
                      >
                      > Shouldn't.
                      >
                      > What do you think happens if the string constructor can't allocate
                      > enough memory?[/color]


                      Asshole, please read both my posts, then and only then return with your
                      usual asshole remarks.


                      -JKop

                      Comment

                      • Richard Herring

                        #12
                        Re: reference to array?

                        In message <QxPad.33242$Z1 4.13173@news.in digo.ie>, JKop <NULL@NULL.NULL >
                        writes[color=blue]
                        >Richard Herring posted:
                        >[color=green]
                        >> In message <c_Mad.33221$Z1 4.12927@news.in digo.ie>, JKop <NULL@NULL.NULL >
                        >> writes[color=darkred]
                        >>>
                        >>>> void blah()
                        >>>> {
                        >>>> string k("salj"); }
                        >>>
                        >>>
                        >>>Should've been:
                        >>>
                        >>>
                        >>>void blah() throw()
                        >>>{
                        >>>
                        >>> string k("salj"); }
                        >>>[/color]
                        >>
                        >> Shouldn't.
                        >>
                        >> What do you think happens if the string constructor can't allocate
                        >> enough memory?[/color]
                        >
                        >
                        >Asshole, please read both my posts,[/color]

                        Life's too short.

                        If you can't take the trouble to say what you mean the first time round,
                        you need to make sure your corrections contain enough context to make
                        your point clear. Otherwise, expect people reading posts in isolation
                        to take them at face value.
                        [color=blue]
                        >then and only then return with your
                        >usual asshole remarks.[/color]

                        If you don't like them, there are obvious solutions.

                        --
                        Richard Herring

                        Comment

                        • Cy Edmunds

                          #13
                          Re: reference to array?

                          "JKop" <NULL@NULL.NULL > wrote in message
                          news:c_Mad.3322 1$Z14.12927@new s.indigo.ie...[color=blue]
                          >[color=green]
                          > > void blah()
                          > > {
                          > > string k("salj");
                          > > }[/color]
                          >
                          >
                          > Should've been:
                          >
                          >
                          > void blah() throw()
                          > {
                          >
                          > string k("salj");
                          > }
                          >
                          >
                          > -JKop[/color]

                          Tell it to Herb:



                          --
                          Cy
                          http://home.rochester.rr.com/cyhome/


                          Comment

                          • -JKop

                            #14
                            Re: reference to array?

                            Cy Edmunds posted:
                            [color=blue]
                            > http://www.gotw.ca/publications/mill22.htm[/color]



                            Interesting! Thanks.


                            -JKop

                            Comment

                            Working...