Subexpression types

K

Kirk Zurell

I'm working through an (older) copy of the C standard to learn if there
is any minute difference between

char * ptr;
/* ... */
*ptr++ = 0x23;

and

char * ptr;
/* ... */
*(ptr)++ = 0x23;

owing to promotion or something obscure I don't know about.

Here's what I've concluded:

An identifier describing an object is an lvalue (6.5.1 2).

A parenthesized expression is an lvalue. Its type and value are
identical to the unparenthesized expression (6.5.1 5).

The operand of the * operator shall have pointer type (6.5.3.2 2) and is
an lvalue (6.5.3.2 4).

So, in *(ptr)++ it's required that the (ptr) expression have pointer
type. Based on this, I think *ptr++ and *(ptr)++ are identical.

Anyone spot me on this?

Thanks
Kirk
 
S

Stefan Ram

Kirk Zurell said:
A parenthesized expression is an lvalue.

Then, the expression »(4)« would be an lvalue?
I think *ptr++ and *(ptr)++ are identical.

This depends on the notion of »identity« used, »identical« is
a vague term without further specification. Obvioulsy, those
two expression differ in their length and structural depth:
So a compiler with a certain implementation limit regarding
expression complexity might accept one but reject the other in
some context.
 
B

Ben Bacarisse

Kirk Zurell said:
I'm working through an (older) copy of the C standard to learn if there
is any minute difference between

char * ptr;
/* ... */
*ptr++ = 0x23;

and

char * ptr;
/* ... */
*(ptr)++ = 0x23;

owing to promotion or something obscure I don't know about.

Here's what I've concluded:

An identifier describing an object is an lvalue (6.5.1 2).

A parenthesized expression is an lvalue.

In this case, yes, but only because ptr is an lvalue.
Its type and value are
identical to the unparenthesized expression (6.5.1 5).

The operand of the * operator shall have pointer type (6.5.3.2 2) and is
an lvalue (6.5.3.2 4).

The operand of * is not (ptr) -- it is (ptr)++ -- but that does not make
much difference in this case.
So, in *(ptr)++ it's required that the (ptr) expression have pointer
type. Based on this, I think *ptr++ and *(ptr)++ are identical.

I think you only need to know that the parentheses have no effect on
the value, type or "kind" of the expression. By "kind" I mean
whether the expression is an lvalue, a function designator or a void
expression.
 
K

Kirk Zurell

Stefan said:
Then, the expression »(4)« would be an lvalue?

Oh bother.
[A] compiler with a certain implementation limit regarding
expression complexity might accept one but reject the other in
some context.

That's a good point. Thanks.

Kirk.
 
K

Kirk Zurell

Ben said:
In this case, yes, but only because ptr is an lvalue.

Double bother. Thanks.
I think you only need to know that the parentheses have no effect on
the value, type or "kind" of the expression. By "kind" I mean
whether the expression is an lvalue, a function designator or a void
expression.

That's what I was hoping to confirm.
Thanks
Kirk.
 

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,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top