473,789 Members | 2,617 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 14726
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******** ********@newssv r27.news.prodig y.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******** ********@newssv r27.news.prodig y.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******** ********@newssv r27.news.prodig y.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
2067
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 << " and &s = " << &s << "\n";
4
2143
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 classes
3
2362
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. ---------------------------------------------- //example 1: typedef int t_Array; int main(int argc, char* argv)
35
2906
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 */ memset(TextureImage,0,sizeof(void *)*1); /* Line 2*/ According to my knowledge in the first line
204
13124
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 = {0,1,2,4,9};
16
2517
by: aegis | last post by:
Given the following: int a = 10; int *p; void *p1; unsigned char *p2; p = &a;
41
10060
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 integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant.55) If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed...
23
7818
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 called, can be a genuine no-op. Consider: typedef int(*polymorphic_func)(int param);
27
8979
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 advance. Erik
8
2237
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
9666
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10139
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9983
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9020
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7529
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4092
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3700
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.