473,324 Members | 2,548 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,324 software developers and data experts.

The result of ++ is not an lvalue?

K&R2, both sec A7.3.4 Postfix Incrementation and secA.7.4.1 Prefix
Incrementation Operators say that "The result is not an lvalue". Is
this correct? I don't see an errata on these points. If below the p++
or ++p is not an lvalue, then *p++ or *++p is a syntax error.

When it talks about Unary Operators, why doesn't the secA7.4 collect
both the prefix and postfix incrementation operators together?

int main(void)
{
char a[10] = {0}, *p = a;

/* a[0] = 'a'; a[1] = 'b'; */
/* *p = 'a'; *(p+1) = 'b'; */
/* *p = 'a'; *p++ = 'b'; p--; */
*p = 'a'; *++p = 'b'; p--;
return 0;
}

May 20 '07 #1
16 3366
lovecreatesbea...@gmail.com wrote:
K&R2, both sec A7.3.4 Postfix Incrementation and secA.7.4.1 Prefix
Incrementation Operators say that "The result is not an lvalue". Is
this correct?
Yes. The result is a value, and specifies nothing about where it is.
An lvalue must have a location associated with it.
I don't see an errata on these points.
Errata are reserved for errors. Why should there be an erratum for
something not in error?
If below the p++
or ++p is not an lvalue, then *p++ or *++p is a syntax error.
Really? Why would you claim that?
When p is a pointer, the result of p++ or ++p is a pointer value which
can of course be dereferenced. Why would you think that only lvalues
can be dereferenced? The '*' indirection operator is applied to
expressions, not just lvalues.
When it talks about Unary Operators, why doesn't the secA7.4 collect
both the prefix and postfix incrementation operators together?
The choice of presentation is up to the authors. We are not mind
readers nor responsible for their choices. Ask them. I'm not even sure
what your question means. Certainly in K&R1 (A.7.2 Unary Operators) the
++lvalue, lvalue--, lvalue++, and lvalue-- are about as close to each
other as is physically possible.
>
int main(void)
{
char a[10] = {0}, *p = a;

/* a[0] = 'a'; a[1] = 'b'; */
/* *p = 'a'; *(p+1) = 'b'; */
/* *p = 'a'; *p++ = 'b'; p--; */
*p = 'a'; *++p = 'b'; p--;
return 0;
}
May 20 '07 #2
"lovecreatesbea...@gmail.com" <lo***************@gmail.comwrites:
K&R2, both sec A7.3.4 Postfix Incrementation and secA.7.4.1 Prefix
Incrementation Operators say that "The result is not an lvalue". Is
this correct?
Absolutely.
I don't see an errata on these points. If below the p++
or ++p is not an lvalue, then *p++ or *++p is a syntax error.
No, the operand of the unary "*" operator doesn't need to be an
lvalue; it just needs to be a pointer value.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
May 20 '07 #3
jg
On May 19, 10:48 pm, "lovecreatesbea...@gmail.com"
<lovecreatesbea...@gmail.comwrote:
K&R2, both sec A7.3.4 Postfix Incrementation and secA.7.4.1 Prefix
Incrementation Operators say that "The result is not an lvalue". Is
this correct? I don't see an errata on these points. If below the p++
Yes, the result is not an lvalue.
or ++p is not an lvalue, then *p++ or *++p is a syntax error.
No. That (p++) isn't an lvalue means that there is no way to
change (p++), and doesn't mean (p++) cannot be dereferenced.

Let's see p = 1, (p++) is also 1. This '1' cannot be changed
in the way that any variable is changed because this '1' does not
have lvalue. You can't do (p++) = 2, for example.

May 20 '07 #4
Keith Thompson said:

<snip>
No, the operand of the unary "*" operator doesn't need to be an
lvalue; it just needs to be a pointer value.
....which must be of object or function type, and which must actually
point to an object or function.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
May 20 '07 #5

"lovecreatesbea...@gmail.com" <lo***************@gmail.comwrote in message
news:11*********************@p77g2000hsh.googlegro ups.com...
K&R2, both sec A7.3.4 Postfix Incrementation and secA.7.4.1 Prefix
Incrementation Operators say that "The result is not an lvalue". Is
this correct? I don't see an errata on these points. If below the p++
or ++p is not an lvalue, then *p++ or *++p is a syntax error.

When it talks about Unary Operators, why doesn't the secA7.4 collect
both the prefix and postfix incrementation operators together?

int main(void)
{
char a[10] = {0}, *p = a;

/* a[0] = 'a'; a[1] = 'b'; */
/* *p = 'a'; *(p+1) = 'b'; */
/* *p = 'a'; *p++ = 'b'; p--; */
*p = 'a'; *++p = 'b'; p--;
return 0;
}
The postfix operators are bit unsusal in that they have high binding
precedence but are applied last.
This allows for concise code in loops

while(*ptr++);

will step through a zero-terminated array, for instance, setting ptr to the
position after the last member.

What this means is that *ptr++ = 0;
sets *ptr to 0, then increments ptr.

However my thinking is moving away from this type of code. Generally now I
put an increment / decrement on its own line. If you've got to think about
how precedence resolves, the code is unnecessarily hard to read.
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

May 20 '07 #6
Malcolm McLean wrote:

(fx:snip)
This allows for concise code in loops

while(*ptr++);

will step through a zero-terminated array, for instance, setting ptr to
the position after the last member.

What this means is that *ptr++ = 0;
sets *ptr to 0, then increments ptr.

However my thinking is moving away from this type of code. Generally now I
put an increment / decrement on its own line. If you've got to think about
how precedence resolves, the code is unnecessarily hard to read.
Or you haven't learnt the idioms.

`*p++` is a C idiom, widely used. You should no more need to
think about how precedence resolves to use it than you should
in `a + b * c`.

[ie, you have to learn it /sometime/, but once learnt, it's
like riding a bicycle -- balancing becomes natural, and you
should always wear a helment in case of accide.]

--
Oops I Did It Hedgehog
"Never ask that question!" Ambassador Kosh, /Babylon 5/

May 20 '07 #7
lovecreatesbea...@gmail.com wrote:
If below the p++
or ++p is not an lvalue, then *p++ or *++p is a syntax error.
Wrong.

There is no requirment for the operand of unary *,
to be an lvalue.

Example:
(p[0]) means the exact same thing as (*(p + 0)).
(p + 0) is not an lvalue.
You can't have ((p + 0) = 0).

--
pete
May 20 '07 #8
Malcolm McLean said:

<snip>
What this means is that *ptr++ = 0;
sets *ptr to 0, then increments ptr.
No, it doesn't. ++ has higher precedence, and so it is the value of
ptr++ that is provided as the operand to *. Of course, the value of
ptr++ is the value that ptr had at the previous sequence point.

The precise order in which stuff happens is up to the implementation, of
course, but the Standard requires the semantics of the expression to be
as I have described.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
May 20 '07 #9
lovecreatesbea...@gmail.com wrote:
K&R2, both sec A7.3.4 Postfix Incrementation and secA.7.4.1 Prefix
Incrementation Operators say that "The result is not an lvalue". Is
this correct? I don't see an errata on these points. If below the p++
or ++p is not an lvalue, then *p++ or *++p is a syntax error.
No. *(any_valid_pointer_expression) is an lvalue, whatever
the status of any_valid_pointer_expression itself.

Even if it were an error, it wouldn't be a syntax error.
There is nothing syntactically wrong with ++TheAnswer = 43;
the semantics are what doom it.
When it talks about Unary Operators, why doesn't the secA7.4 collect
both the prefix and postfix incrementation operators together?
You'd need to ask K or R for their exact reasons, but it
seems plausible that they separated the postfix operators from
the prefix operators because the postfix operators are not in
the same class and have a higher precedence. Oddly enough,
the Standard makes the same separation.

--
Eric Sosman
es*****@acm-dot-org.invalid
May 20 '07 #10
Malcolm McLean wrote:
>
The postfix operators are bit unsusal in that they have high binding
precedence but are applied last.
This is complete nonsense.
This allows for concise code in loops

while(*ptr++);

will step through a zero-terminated array, for instance, setting ptr to
the position after the last member.

What this means is that *ptr++ = 0;
sets *ptr to 0, then increments ptr.
Wrong again. The time when ptr is incremented is not
specified; indeed, the incrementation may take "macroscopic
time" and overlap with other operations bracketed by the
same pair of sequence points. Regardless of the scheduling
of the increment, though, the value of ptr++ is defined
as the value of ptr as it was before the ++ operator caused
any change. That value is what the * operator uses, and
the lvalue produced by * is what the = operator uses. Neither
the = nor the * use the "current" value of ptr in any way.
However my thinking is moving away from this type of code. Generally now
I put an increment / decrement on its own line. If you've got to think
about how precedence resolves, the code is unnecessarily hard to read.
The third sentence is true, but if "you've got to think"
about precedence when reading something as simple as *p++, it
means your ability to read C has not yet transcended the level
of "LOOK, JANE. SEE SPOT. SEE SPOT RUN."

--
Eric Sosman
es*****@acm-dot-org.invalid

May 20 '07 #11
Richard Heathfield <rj*@see.sig.invalidwrites:
Malcolm McLean said:

<snip>
>What this means is that *ptr++ = 0;
sets *ptr to 0, then increments ptr.

No, it doesn't. ++ has higher precedence, and so it is the value of
ptr++ that is provided as the operand to *. Of course, the value of
ptr++ is the value that ptr had at the previous sequence point.
What?

Are you really telling me that the most famous string copy in the world
doesn't work im comp.lang.c land?

while(*dest++=*src++);

or you making life difficult for human beings again?

His text above is pretty clear and makes sense.

*ptr is indeed set to 0 and then (in terms of this sentence) ptr is
incremented.

Or?!?!?! Am I losing my senses?
May 21 '07 #12
Richard said:
Richard Heathfield <rj*@see.sig.invalidwrites:
>Malcolm McLean said:

<snip>
>>What this means is that *ptr++ = 0;
sets *ptr to 0, then increments ptr.

No, it doesn't. ++ has higher precedence, and so it is the value of
ptr++ that is provided as the operand to *. Of course, the value of
ptr++ is the value that ptr had at the previous sequence point.

What?

Are you really telling me that the most famous string copy in the
world doesn't work im comp.lang.c land?

while(*dest++=*src++);
Not at all. If you think so, you have misinterpreted what I wrote.
or you making life difficult for human beings again?
Not at all. I'm actually trying to make life /easier/, by making clear
what the Standard actually guarantees about the expression *ptr++ = 0;
His text above is pretty clear and makes sense.
Sure. It's just wrong, that's all, although this isn't quite as clear
from the quoted text alone as from the article in which it was
originally said:

"The postfix operators are bit unsusal in that they have high binding
precedence but are applied last."

That was the viewpoint he was trying to explain, and that viewpoint is
incorrect.
*ptr is indeed set to 0 and then (in terms of this sentence) ptr is
incremented.
That isn't guaranteed by the Standard. For example, the following
pseudo-assembly-language would be a perfectly legal translation:

MOV tmp, ptr
INC ptr
MOV *tmp, 0

In other words, the increment can happen first, and the update
afterwards. Whether this actually happens depends on the implementor.
The Standard certainly permits it.
Or?!?!?! Am I losing my senses?
I couldn't possibly comment.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
May 21 '07 #13

"Richard Heathfield" <rj*@see.sig.invalidwrote in message
news:-9******************************@bt.com...
Richard said:
>Richard Heathfield <rj*@see.sig.invalidwrites:
>>Malcolm McLean said:

<snip>

What this means is that *ptr++ = 0;
sets *ptr to 0, then increments ptr.

No, it doesn't. ++ has higher precedence, and so it is the value of
ptr++ that is provided as the operand to *. Of course, the value of
ptr++ is the value that ptr had at the previous sequence point.

What?

Are you really telling me that the most famous string copy in the
world doesn't work im comp.lang.c land?

while(*dest++=*src++);

Not at all. If you think so, you have misinterpreted what I wrote.
>or you making life difficult for human beings again?

Not at all. I'm actually trying to make life /easier/, by making clear
what the Standard actually guarantees about the expression *ptr++ = 0;
>His text above is pretty clear and makes sense.

Sure. It's just wrong, that's all, although this isn't quite as clear
from the quoted text alone as from the article in which it was
originally said:

"The postfix operators are bit unsusal in that they have high binding
precedence but are applied last."

That was the viewpoint he was trying to explain, and that viewpoint is
incorrect.
Explain. Not define.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

May 21 '07 #14
On May 21, 2:18 am, Eric Sosman <esos...@acm-dot-org.invalidwrote:
>
No. *(any_valid_pointer_expression) is an lvalue, whatever
the status of any_valid_pointer_expression itself.
Except when it doesn't designate an object (e.g. if
the any_valid_pointer_expression is pointing one off
the end of an array).

May 21 '07 #15
Old Wolf wrote On 05/21/07 17:31,:
On May 21, 2:18 am, Eric Sosman <esos...@acm-dot-org.invalidwrote:
> No. *(any_valid_pointer_expression) is an lvalue, whatever
the status of any_valid_pointer_expression itself.


Except when it doesn't designate an object (e.g. if
the any_valid_pointer_expression is pointing one off
the end of an array).
One thing about c.l.c. is that verbal shortcuts always
catch up with one ... I used "valid" suggestively rather
than precisely, and dangitall if'n somebody didn't call me
on it.

Another way a perfectly good pointer value can fail to
be "valid" in my imprecise sense is to be a pointer to an
incomplete type, e.g., a `void*' or a `struct opaque*'.
Such a pointer can be perfectly well-defined, useful, and
a model citizen in every way, yet not be capable of yielding
an lvalue.

And if somebody nit-picks that `struct opaque' might
not be an incomplete type, I'm gonna boot up my DS9K and
do some DAMAGE!

http://dialspace.dial.pipex.com/town/green/gfd34/art/

--
Er*********@sun.com
May 21 '07 #16
Old Wolf wrote:
On May 21, 2:18 am, Eric Sosman <esos...@acm-dot-org.invalidwrote:

No. *(any_valid_pointer_expression) is an lvalue, whatever
the status of any_valid_pointer_expression itself.

Except when it doesn't designate an object (e.g. if
the any_valid_pointer_expression is pointing one off
the end of an array).
Even then, and rightly so. Just imagine, otherwise whether

static void inc(int *p) {
++*p;
}

is a constraint violation (remember, a diagnostic is required for
constraint violations) depends on what values of p will be passed to
it at runtime.

May 21 '07 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Hongzheng Wang | last post by:
In K&R, they said: An object is a named region of storage; an lvalue is an expression refer to an object. How about these concept in C++? `The C++ Programming Language' has a similar...
2
by: Xiangliang Meng | last post by:
Hi, all. What will we get from sizeof(a class without data members and virtual functions)? For example: class abnormity { public: string name() { return "abnormity"; }
47
by: fb | last post by:
Hi Everyone. Thanks for the help with the qudratic equation problem...I didn't think about actually doing the math...whoops. Anyway... I'm having some trouble getting the following program to...
24
by: Romeo Colacitti | last post by:
Hi, Does anyone here have a strong understanding for the meanings of the terms "lvalue" and "rvalue" as it pertains to C, objects, and different contexts? If so please share. I've been...
7
by: Andy Lomax | last post by:
The C99 standard contains various statements like this one (in this case, 6.5.16, assignment operator): >If an attempt is made to modify >the result of an assignment operator or to access it...
3
by: Kavya | last post by:
Can someone give and explain in simple terms a definition of lvalue? Also what are these modifiable and non-modifiable lvalues? I always thought that, if we can assign to anything then that...
21
by: Steven T. Hatton | last post by:
I'm trying to improve my formal understanding of C++. One significant part of that effort involves clarifying my understanding of the vocabulary used to describe the language. This is from the...
14
by: nobrow | last post by:
Yes I know what lvalue means, but what I want to ask you guys about is what are all valid lvalues ... a *a a *(a + 1) .... What else?
3
by: donjoe | last post by:
(http://www.thescripts.com/forum/thread649384.html) I saw several people in that thread agree that (++something) is not an lvalue (but rather a constant value) according to K&R... ... but I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.