Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > have question on 'passing array to reference to pointer'

Reply
Thread Tools

have question on 'passing array to reference to pointer'

 
 
ljh131
Guest
Posts: n/a
 
      03-25-2009
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.

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      03-25-2009
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
 
Reply With Quote
 
 
 
 
Pascal J. Bourguignon
Guest
Posts: n/a
 
      03-25-2009
ljh131 <(E-Mail Removed)> 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__
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      03-25-2009
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).
 
Reply With Quote
 
ljh131
Guest
Posts: n/a
 
      03-26-2009
On 3월26일, 오전5시31분, (E-Mail Removed) (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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using an array to reference a different array Andrew Poulos Javascript 6 12-23-2008 02:58 PM
asp.net 2005 question re: "reference to a non-shared member requires an object reference" ce ASP .Net 1 06-23-2005 09:15 PM
Why I can't have a reference to an unsized array ? Timothy Madden C++ 9 11-16-2004 12:34 PM
Reference to array, NOT array of references! JKop C++ 12 07-21-2004 10:51 PM
Referring to a 'column' from a reference to an array of array references fatted Perl Misc 2 10-23-2003 03:22 PM



Advertisments