have question on 'passing array to reference to pointer'

L

ljh131

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.
 
V

Victor Bazarov

ljh131 said:
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
 
P

Pascal J. Bourguignon

ljh131 said:
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;
}
 
J

Juha Nieminen

ljh131 said:
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).
 
L

ljh131

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top