473,395 Members | 1,440 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Pointer-reference question

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
4 14702
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

"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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Chris Mantoulidis | last post by:
I see some really weird output from this program (compiled with GCC 3.3.2 under Linux). #include <iostream> using namespace std; int main() { char *s; s = "test1"; cout << "s = " << s << "...
4
by: Carsten Spieß | last post by:
Hello all, i have a problem with a template constructor I reduced my code to the following (compiled with gcc 2.7.2) to show my problem: // a base class class Base{}; // two derived...
3
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ...
35
by: tuko | last post by:
Hello kind people. Can someone explain please the following code? /* Create Storage Space For The Texture */ AUX_RGBImageRec *TextureImage; /* Line 1*/ /* Set The Pointer To NULL...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
16
by: aegis | last post by:
Given the following: int a = 10; int *p; void *p1; unsigned char *p2; p = &a;
41
by: Alexei A. Frounze | last post by:
Seems like, to make sure that a pointer doesn't point to an object/function, NULL (or simply 0) is good enough for both kind of pointers, data pointers and function pointers as per 6.3.2.3: 3 An...
23
by: bluejack | last post by:
Ahoy... before I go off scouring particular platforms for specialized answers, I thought I would see if there is a portable C answer to this question: I want a function pointer that, when...
27
by: Erik de Castro Lopo | last post by:
Hi all, The GNU C compiler allows a void pointer to be incremented and the behaviour is equivalent to incrementing a char pointer. Is this legal C99 or is this a GNU C extention? Thanks in...
8
by: Martin Jørgensen | last post by:
Hi, "C primer plus" p.382: Suppose we have this declaration: int (*pa); int ar1; int ar2; int **p2;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.