& *

L

Lanarcam

Hi,

I couldn't find the answer in the archives to this question :

Does the C language define this instruction?

int j;
int * pi = &j;

int k = & * pi;
 
L

Lanarcam

Le 02/04/2012 20:38, Lanarcam a écrit :
Hi,

I couldn't find the answer in the archives to this question :

Does the C language define this instruction?

int j;
int * pi = &j;

int k = & * pi;

Sorry, one must read:

int * pk = & * pi;
 
J

James Kuyper

Le 02/04/2012 20:38, Lanarcam a écrit :

Sorry, one must read:

int * pk = & * pi;

The C standard defines the behavior of that construct: "The unary &
operator yields the address of its operand. ... If the operand is the
result of a unary * operator, neither that operator nor the & operator
is evaluated and the result is as if both were omitted, except that the
constraints on the operators still apply and the result is not an
lvalue." (6.5.2p3).

In other words, so long as pi is a pointer variable, &*pi is equivalent
to pi itself, except that &*pi can't be used as an lvalue. In this
context, that means that &*pi == &j is true, but that &*pi = &j would be
a constraint violation.
 
B

Ben Bacarisse

Lanarcam said:
Le 02/04/2012 20:38, Lanarcam a écrit :
Sorry, one must read:

int * pk = & * pi;

Yes, though it's not an instruction in a very real sense. The language
definition says that when the operand of & is a * expression (I'm
abbreviating the wording) both operators are ignored. Your final
declaration is that same as

int *pk = pi;
 
L

Lanarcam

Le 02/04/2012 21:18, James Kuyper a écrit :
The C standard defines the behavior of that construct: "The unary&
operator yields the address of its operand. ... If the operand is the
result of a unary * operator, neither that operator nor the& operator
is evaluated and the result is as if both were omitted, except that the
constraints on the operators still apply and the result is not an
lvalue." (6.5.2p3).

In other words, so long as pi is a pointer variable,&*pi is equivalent
to pi itself, except that&*pi can't be used as an lvalue. In this
context, that means that&*pi ==&j is true, but that&*pi =&j would be
a constraint violation.

Thanks a lot. That question comes from a tool provider which generates
C code, we have a nasty bug and thought that could be the reason, but
from your answer it appears that we must look elsewhere.

(unless the compiler dosn't respect the C standard of course)
 
L

Lanarcam

Le 02/04/2012 21:21, Ben Bacarisse a écrit :
Yes, though it's not an instruction in a very real sense. The language
definition says that when the operand of& is a * expression (I'm
abbreviating the wording) both operators are ignored. Your final
declaration is that same as

int *pk = pi;
Thanks
 
J

jacob navia

Le 02/04/12 21:18, James Kuyper a écrit :
The C standard defines the behavior of that construct: "The unary&
operator yields the address of its operand. ... If the operand is the
result of a unary * operator, neither that operator nor the& operator
is evaluated and the result is as if both were omitted, except that the
constraints on the operators still apply and the result is not an
lvalue." (6.5.2p3).

Make that 6.5.3.2, or maybe you are quoting from an older standard?
 

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
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top