have question on 'passing array to reference to pointer'

Discussion in 'C++' started by ljh131, Mar 25, 2009.

  1. ljh131

    ljh131 Guest

    i have a simple question on passing the array to the reference to
    pointer.

    here is some code.


    void donothing(int*& testarray) {}

    int main()
    {
    int k[8];

    donothing(k); // error!

    return 0;
    }


    compiler says passing the array to reference to pointer cannot be
    done. because k is the array and 'donothing' function takes reference
    to pointer.

    but i want to know why exactly c++ prohibits this casting. someone say
    array should be considered as 'const pointer'.

    and if this casting is illegal, we can change the testarray in the
    function 'donothing' and it leads changing the address of 'k' in the
    main function.

    if you have an idea, just tell me know your opinion, thanks in
    advance.
     
    ljh131, Mar 25, 2009
    #1
    1. Advertising

  2. ljh131 wrote:
    > i have a simple question on passing the array to the reference to
    > pointer.
    >
    > here is some code.
    >
    >
    > void donothing(int*& testarray) {}
    >
    > int main()
    > {
    > int k[8];
    >
    > donothing(k); // error!
    >
    > return 0;
    > }
    >
    >
    > compiler says passing the array to reference to pointer cannot be
    > done. because k is the array and 'donothing' function takes reference
    > to pointer.
    >
    > but i want to know why exactly c++ prohibits this casting. someone say
    > array should be considered as 'const pointer'.
    >
    > and if this casting is illegal, we can change the testarray in the
    > function 'donothing' and it leads changing the address of 'k' in the
    > main function.


    The idea is simple. Array-to-pointer conversion results in an rvalue.
    A reference to non-const cannot be bound to an rvalue. A reference to
    const, OTOH, can. That's why

    void donothing (int* const& testarray) {}

    should do it.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Mar 25, 2009
    #2
    1. Advertising

  3. ljh131 <> writes:

    > i have a simple question on passing the array to the reference to
    > pointer.
    >
    > here is some code.
    >
    >
    > void donothing(int*& testarray) {}
    >
    > int main()
    > {
    > int k[8];
    >
    > donothing(k); // error!
    >
    > return 0;
    > }



    For reference, the following code removes the error:

    void donothing(int*& testptr) {}

    int main()
    {
    int k[8];
    int* kp=k;

    donothing(kp); // ok!

    return 0;
    }


    > compiler says passing the array to reference to pointer cannot be
    > done. because k is the array and 'donothing' function takes reference
    > to pointer.
    >
    > but i want to know why exactly c++ prohibits this casting. someone say
    > array should be considered as 'const pointer'.


    Notice that with:
    int k[8]; int* kp;
    you can write:
    kp=k;
    k is a r-value compatible with type int*.

    But you could not write k=kp; This would be meaningless since kp is a
    pointer, but k is an array of int.
    k is not a l-value of type int*.


    > and if this casting is illegal, we can change the testarray in the
    > function 'donothing' and it leads changing the address of 'k' in the
    > main function.


    For references, you must pass l-values, not r-values. Since you
    defined it as a reference to a pointer to a int, testarray is not an
    array but a pointer. You must give it a variable holding a pointer to
    int.

    Notice that a function could change the reference. How could it work
    if you passed an array variable instead of a pointer variable?


    static int k1[8];
    static int k2[8];
    void doSomething(int*& kp){ if(rand()%2){ kp=k1; }else{ kp=k2; } }

    int main()
    {
    int k[8];
    int* kp=k;

    doSomething(kp);

    return 0;
    }


    --
    __Pascal Bourguignon__
     
    Pascal J. Bourguignon, Mar 25, 2009
    #3
  4. ljh131 wrote:
    > but i want to know why exactly c++ prohibits this casting. someone say
    > array should be considered as 'const pointer'.


    I assume you know the difference between "const pointer" and "pointer
    to const".

    The latter is a pointer variable, which has been declared to point to
    a value which cannot be modified through that pointer (ie. the pointer
    points to a const).

    The former means that the pointer variable itself is const (rather
    than the value it's pointing to). In other words, you cannot modify the
    pointer itself to point somewhere else.

    You can get a long way by, indeed, thinking that array names act like
    const pointers (ie. pointer variables which cannot be changed).

    You can't give a const variable as parameter to a function taking a
    non-const reference to a variable of that type. That simply breaks
    constness. Thus you can't give an array as a parameter to a function
    taking a non-const pointer reference. (By using the non-const reference
    the function could be able to modify the array "pointer" to point
    somewhere else, which not only breaks constness, but especially with
    arrays doesn't even make sense.)

    You can give an array as parameter to a function taking a pointer.
    That's because you are not really giving the array to the function, but
    a pointer to the first element of the array (the language implicitly
    makes such a conversion without you having to specify it explicitly).
     
    Juha Nieminen, Mar 25, 2009
    #4
  5. ljh131

    ljh131 Guest

    On 3¿ù26ÀÏ, ¿ÀÀü5½Ã31ºÐ, (blargg) wrote:
    > Juha Nieminen wrote:
    > > ljh131 wrote:
    > > > but i want to know why exactly c++ prohibits this casting. someone say
    > > > array should be considered as 'const pointer'.

    >
    > > I assume you know the difference between "const pointer" and "pointer
    > > to const".

    >
    > > The latter is a pointer variable, which has been declared to point to
    > > a value which cannot be modified through that pointer (ie. the pointer
    > > points to a const).

    >
    > > The former means that the pointer variable itself is const (rather
    > > than the value it's pointing to). In other words, you cannot modify the
    > > pointer itself to point somewhere else.

    >
    > > You can get a long way by, indeed, thinking that array names act like
    > > const pointers (ie. pointer variables which cannot be changed).

    >
    > [...]
    >
    > You can get all the way by simply noting that an array can be implicitly
    > converted into a pointer to its first element, similar to how a double can
    > be implicitly converted into an int. In both cases, you can't take a
    > non-const reference to this implicit conversion:
    >
    > // using int and double
    > typedef double T;
    > typedef int U;
    > void f( U& );
    > void fc( U const& );
    >
    > T t;
    > void example()
    > {
    > f( t ); // error
    > fc( t ); // OK, creates temporary
    >
    > U const& temp = t; // equivalent to previous statement
    > fc( temp );
    > }
    >
    > // using array
    > typedef int T [2];
    > typedef int* U;
    > void f( U& );
    > void fc( U const& );
    >
    > T t;
    > void example()
    > {
    > f( t ); // error
    > fc( t ); // OK, creates temporary
    >
    > U const& temp = t; // equivalent to previous statement
    > fc( temp );
    > }
    >
    > The code after the typedefs is identical in both cases.


    thanks for all of your answers.
     
    ljh131, Mar 26, 2009
    #5
    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. sam pal
    Replies:
    3
    Views:
    550
    E. Robert Tisdale
    Jul 16, 2003
  2. entitledX
    Replies:
    4
    Views:
    514
    Old Wolf
    Jan 12, 2006
  3. jimjim
    Replies:
    16
    Views:
    844
    Jordan Abel
    Mar 28, 2006
  4. PeterOut

    Passing a 2D array as a pointer to a pointer

    PeterOut, Aug 27, 2008, in forum: C Programming
    Replies:
    3
    Views:
    306
    Nick Keighley
    Aug 28, 2008
  5. A
    Replies:
    7
    Views:
    639
Loading...

Share This Page