By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,406 Members | 1,028 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,406 IT Pros & Developers. It's quick & easy.

Pointer-reference question

P: n/a
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
Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
BCC wrote:
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).
Jul 19 '05 #2

P: n/a

"BCC" <a@b.c> wrote in message
news:0M****************@newssvr27.news.prodigy.com ...
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
Jul 19 '05 #3

P: n/a
"BCC" <a@b.c> wrote in message
news:0M****************@newssvr27.news.prodigy.com ...
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

Jul 19 '05 #4

P: n/a
David White wrote:
"BCC" <a@b.c> wrote in message
news:0M****************@newssvr27.news.prodigy.com ...
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.

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.

Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.