Why can't the source compile?

Discussion in 'C++' started by sword, Aug 23, 2006.

  1. sword

    sword Guest

    void test(int*& a);


    int main()
    {
    int a[4];
    test(a);
    return 0;
    }



    Who can tell me the reason why the source above can't compile?
    I use VC++ 6.0.
    test.cpp(7) : error C2664: 'test' : cannot convert parameter 1 from
    'int [4]' to 'int *& '
    sword, Aug 23, 2006
    #1
    1. Advertising

  2. sword

    Phlip Guest

    sword wrote:

    > void test(int*& a);


    a is a reference to a pointer. That means you must pass a pointer, for the
    reference to seat on.

    > int a[4];
    > test(a);


    a is not a pointer, it's an array.

    Either take out the & (most likely what you need), or introduce a pointer:

    int * p = a;
    test(p);

    And you might want to read ahead in your tutorial, to soak this stuff in...

    --
    Phlip
    http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
    Phlip, Aug 23, 2006
    #2
    1. Advertising

  3. sword

    sword Guest

    But if I write the function like this:

    void test(int* a);


    It will be OK.
    sword, Aug 23, 2006
    #3
  4. sword

    Mark P Guest

    Phlip wrote:
    > sword wrote:
    >
    >> void test(int*& a);

    >
    > a is a reference to a pointer. That means you must pass a pointer, for the
    > reference to seat on.
    >
    >> int a[4];
    >> test(a);

    >
    > a is not a pointer, it's an array.
    >


    But it can decay to a pointer. The issue is that when it's viewed as a
    pointer, it's a constant pointer (i.e., it must point to the same place
    always). And you can't pass "T const" as "T&" since the constant
    qualifier is discarded someone may attempt to modify the T&.

    Rewrite the declaration of test as:

    void test( int* const& a );

    and it should compile.


    -Mark
    Mark P, Aug 23, 2006
    #4
  5. sword

    Ian Collins Guest

    sword wrote:
    > void test(int*& a);
    >
    >
    > int main()
    > {
    > int a[4];
    > test(a);
    > return 0;
    > }
    >
    >
    >
    > Who can tell me the reason why the source above can't compile?


    You can pass an array of int to a function with int* as its parameter,
    but there isn't an automatic conversion from an array of int to an int
    pointer reference.

    --
    Ian Collins.
    Ian Collins, Aug 23, 2006
    #5
  6. sword

    Guest

    void test(int* a);


    int main()
    {
    int a[4];
    test(a);
    return 0;
    }


    sword wrote:
    > void test(int*& a);
    >
    >
    > int main()
    > {
    > int a[4];
    > test(a);
    > return 0;
    > }
    >
    >
    >
    > Who can tell me the reason why the source above can't compile?
    > I use VC++ 6.0.
    > test.cpp(7) : error C2664: 'test' : cannot convert parameter 1 from
    > 'int [4]' to 'int *& '
    , Aug 23, 2006
    #6
  7. sword

    Greg Comeau Guest

    In article <p4UGg.12315$>,
    Mark P <> wrote:
    >Phlip wrote:
    >> sword wrote:
    >>
    >>> void test(int*& a);

    >>
    >> a is a reference to a pointer. That means you must pass a pointer, for the
    >> reference to seat on.
    >>
    >>> int a[4];
    >>> test(a);

    >>
    >> a is not a pointer, it's an array.
    >>

    >
    >But it can decay to a pointer. The issue is that when it's viewed as a
    >pointer, it's a constant pointer (i.e., it must point to the same place
    >always). And you can't pass "T const" as "T&" since the constant
    >qualifier is discarded someone may attempt to modify the T&.
    >
    >Rewrite the declaration of test as:
    >
    >void test( int* const& a );
    >
    >and it should compile.


    This code looks right, and brings the elaboration full circle.
    I would suggest though that the OP avoid this because unless
    I'm missing something obvious there does not seem to be anything
    gained over int * in this case.
    --
    Greg Comeau / 20 years of Comeauity! Intel Mac Port now in alpha!
    Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
    World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
    Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
    Greg Comeau, Aug 23, 2006
    #7
  8. sword

    Ron Natalie Guest

    Mark P wrote:

    >
    > But it can decay to a pointer. The issue is that when it's viewed as a
    > pointer, it's a constant pointer (i.e., it must point to the same place
    > always).


    No it's not. When it is a pointer, it is an rvalue that has been
    converted form the array. It's type is NOT const (although it's
    not possible to legally modify it).

    > And you can't pass "T const" as "T&" since the constant
    > qualifier is discarded someone may attempt to modify the T&.


    There's no "const" qualifier here to discard. The issue is that
    you can not bind non-const references to rvalues regardless of
    the constness of the rvalue.

    >
    > Rewrite the declaration of test as:
    >
    > void test( int* const& a );
    >
    > and it should compile.
    >

    This is correct.
    Ron Natalie, Aug 23, 2006
    #8
  9. sword posted:

    > void test(int*& a);
    >
    >
    > int main()
    > {
    > int a[4];
    > test(a);
    > return 0;
    > }



    An array can decay to a pointer to its first element -- however, the pointer
    it yields is an R-value. A reference to non-const cannot bind to an R-value.

    --

    Frederick Gotham
    Frederick Gotham, Aug 23, 2006
    #9
    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. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    863
    Mark Rae
    Dec 21, 2006
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,766
    Smokey Grindel
    Dec 2, 2006
  3. jing
    Replies:
    1
    Views:
    312
    mlimber
    Dec 8, 2005
  4. Nagaraj
    Replies:
    1
    Views:
    842
    Lionel B
    Mar 1, 2007
  5. fAnSKyer
    Replies:
    2
    Views:
    519
    Alf P. Steinbach
    Jun 7, 2009
Loading...

Share This Page