reference to array?

Discussion in 'C++' started by Gernot Frisch, Oct 11, 2004.

  1. 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%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

    ________________________________________
    Looking for a good game? Do it yourself!
    GLBasic - you can do
    www.GLBasic.com
     
    Gernot Frisch, Oct 11, 2004
    #1
    1. Advertising

  2. "Gernot Frisch" <> wrote in message
    news:...
    > Hope you know what I mean...
    >
    >
    > void Work(int& r[4])


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

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

    john
     
    John Harrison, Oct 11, 2004
    #2
    1. Advertising

  3. Gernot Frisch

    Ron Natalie Guest

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


    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.
     
    Ron Natalie, Oct 11, 2004
    #3
  4. Gernot Frisch

    JKop Guest

    Gernot Frisch posted:

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


    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
     
    JKop, Oct 11, 2004
    #4
  5. > Your code will work the way you expect if you define the
    > function as:
    > void Work(int r[4]) {


    How can I happen to be so silly to have forgotten that... Thank you
    very much.
     
    Gernot Frisch, Oct 11, 2004
    #5
  6. Gernot Frisch

    DaKoadMunky Guest

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


    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.
     
    DaKoadMunky, Oct 12, 2004
    #6
  7. Gernot Frisch

    Cy Edmunds Guest

    "JKop" <> wrote in message
    news:64xad.33165$...
    > Gernot Frisch posted:
    >
    > > Hope you know what I mean...
    > >
    > >
    > > void Work(int& r[4])
    > > {
    > > r[0]=0;
    > > }
    > >
    > > int main(int, char**)
    > > {
    > > int a[4]
    > > Work(a);
    > > }
    > >

    >
    > 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;
    > }
    >


    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/
     
    Cy Edmunds, Oct 12, 2004
    #7
  8. Gernot Frisch

    JKop Guest


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

    >
    > I think const is a little misleading here.


    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.

    > The function gets a copy of
    > the pointer anyway so the caller wouldn't care if the function changed
    > its value.


    Agreed.

    > Also, I see you have discovered throw specifications.


    Indeed.

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


    I know exactly how it works. If this particular function threw an exception,
    then the function "std::unexpected" 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
     
    JKop, Oct 12, 2004
    #8
  9. Gernot Frisch

    JKop Guest


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



    Should've been:


    void blah() throw()
    {

    string k("salj");
    }


    -JKop
     
    JKop, Oct 12, 2004
    #9
  10. In message <c_Mad.33221$>, JKop <>
    writes
    >
    >> void blah()
    >> {
    >> string k("salj");
    >> }

    >
    >
    >Should've been:
    >
    >
    >void blah() throw()
    >{
    >
    > string k("salj");
    >}
    >


    Shouldn't.

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

    --
    Richard Herring
     
    Richard Herring, Oct 12, 2004
    #10
  11. Gernot Frisch

    JKop Guest

    Richard Herring posted:

    > In message <c_Mad.33221$>, JKop <>
    > writes
    >>
    >>> void blah()
    >>> {
    >>> string k("salj"); }

    >>
    >>
    >>Should've been:
    >>
    >>
    >>void blah() throw()
    >>{
    >>
    >> string k("salj"); }
    >>

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



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


    -JKop
     
    JKop, Oct 12, 2004
    #11
  12. In message <QxPad.33242$>, JKop <>
    writes
    >Richard Herring posted:
    >
    >> In message <c_Mad.33221$>, JKop <>
    >> writes
    >>>
    >>>> void blah()
    >>>> {
    >>>> string k("salj"); }
    >>>
    >>>
    >>>Should've been:
    >>>
    >>>
    >>>void blah() throw()
    >>>{
    >>>
    >>> string k("salj"); }
    >>>

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

    >
    >
    >Asshole, please read both my posts,


    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.

    >then and only then return with your
    >usual asshole remarks.


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

    --
    Richard Herring
     
    Richard Herring, Oct 12, 2004
    #12
  13. Gernot Frisch

    Cy Edmunds Guest

    Cy Edmunds, Oct 12, 2004
    #13
  14. Gernot Frisch

    -JKop Guest

    -JKop, Oct 12, 2004
    #14
    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. JKop
    Replies:
    12
    Views:
    718
    Howard
    Jul 21, 2004
  2. Mara Guida

    const and array of array (of array ...)

    Mara Guida, Sep 2, 2009, in forum: C Programming
    Replies:
    3
    Views:
    496
    David RF
    Sep 3, 2009
  3. fatted
    Replies:
    2
    Views:
    146
    James Willmore
    Oct 23, 2003
  4. Tom
    Replies:
    3
    Views:
    213
    salsablr
    Dec 20, 2004
  5. Andrew Poulos

    Using an array to reference a different array

    Andrew Poulos, Dec 22, 2008, in forum: Javascript
    Replies:
    6
    Views:
    85
    Jorge
    Dec 23, 2008
Loading...

Share This Page