Pointer-reference question

B

BCC

Can someone please explain this to me... if I have:

int x = 10;

I can do:
int* p_x = &x;

Compiles okay.

If I have a function though:
int ReturnInt() { return x; }
// Where x = 10;

and then do:

int* p_x = &ReturnInt();

I get a compiler error "C2102 '&' requires l-value".

I had thought that these should be the same. Clearly they are not, but Im
not sure why.

Thanks,
B
 
G

Gianni Mariani

BCC said:
Can someone please explain this to me... if I have:

int x = 10;

I can do:
int* p_x = &x;

Compiles okay.

If I have a function though:
int ReturnInt() { return x; }
// Where x = 10;

and then do:

int* p_x = &ReturnInt();

I get a compiler error "C2102 '&' requires l-value".

I had thought that these should be the same. Clearly they are not, but Im
not sure why.

You're taking the address of a return value ?

Take this for example:

int * p = &(5+5);

In this case, 10 is not stored in memory, it's the result of an
expression and never is stored in the address space of the program so
you can't take the address of it. Also the return value from a function
is also not allways addressible as a pointer.

Anyhow, the issue really is that the DEFINITION OF THE LANGUAGE does not
permit you to take the address of a non "l-value" which is defined in
the language.

An l-value (left hand side value) is an expression that refers to an
object (or pod).
 
T

Thomas Wintschel

BCC said:
Can someone please explain this to me... if I have:

int x = 10;

I can do:
int* p_x = &x;

Compiles okay.

If I have a function though:
int ReturnInt() { return x; }
// Where x = 10;

and then do:

int* p_x = &ReturnInt();

I get a compiler error "C2102 '&' requires l-value".

I had thought that these should be the same. Clearly they are not, but Im
not sure why.

Thanks,
B

The return value of ReturnInt() is a temporary object. You can assign it to
something, use it to initialize something etc. but you cannot safely take
it's
address since it will cease to exist once the current line of code is
executed.

Tom
 
D

David White

BCC said:
Can someone please explain this to me... if I have:

int x = 10;

I can do:
int* p_x = &x;

Compiles okay.

If I have a function though:
int ReturnInt() { return x; }
// Where x = 10;

and then do:

int* p_x = &ReturnInt();

I get a compiler error "C2102 '&' requires l-value".

I had thought that these should be the same. Clearly they are not, but Im
not sure why.

Others have already answered. I just wanted to point out that your question
is entirely about addresses and pointers, and is completely unrelated to
references.

DW
 
G

Gianni Mariani

David said:
Others have already answered. I just wanted to point out that your question
is entirely about addresses and pointers, and is completely unrelated to
references.


good point ...

I was thinking of mentioning that, but I decided to keep from getting
too confusing.

For example:

int x = 10;

int & Func()
{
return x;
}

int main()
{
Func() = 11;

int * p = & ( Func() );

}

Note that in this case, Func() is returning a "reference" - in this case
it IS an lvalue. You need to be extra careful that the reference is
pointing to an object that is not destoyed for the life of the reference.
 

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

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
EmeliaBryc

Latest Threads

Top