7

Why is the following illegal in C?

y = (w + x)++;

According to my book, this is illegal, but I don't see why.

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353

7 Answers7

21

In i++, the value of i is changed. After execution, i's value is one plus its previous value. You can't store a value in w+x, though, and so you can't do any of the following, which all (if they worked) would have more or less the same effect:

w+x = w+x+1;
w+x += 1;
(w+x)++;

Something that can be placed on the left hand side of an assignment is typically called an lvalue (l is for left). The concise way of saying this is that ++ can only be applied to lvalues, and w+x isn't an lvalue. You can read more about lvalues (and other kinds of values) in this question and its answers:

Community
  • 1
  • 1
Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
8

According to Dennis M. Ritchie's book: "The C Programming Language":

2.8 Increment and Decrement Operators

(page 44)

The increment and decrement operators can only be applied to variables; an expression like (i + j)++ is illegal. The operand must be a modifiable lvalue of arithmetic or pointer type.

Because an expression

i++;

is equivalent to:

i = i + 1;

So an expression like:

(i + j)++;

something like equivalent to:

(i + j) =  (i + j) + 1;  // meaning less

That looks meaning less, we can't modify expression.

Related: An interesting bug one may like to know about in gcc 4.4.5 is that expression j = ++(i | i); compiles that should produce l-value error. Read: j = ++(i | i); and j = ++(i & i); should an error: lvalue?

Read about modifiable lvalue from (1) Expression must be a modifiable lvalue error and (2) msdn docs: Lvalues and Rvalues

Community
  • 1
  • 1
Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
  • 1
    It's good to cite sources (and this is good one), but I wish the book didn't say “variables,” though, because there are more lvalues than there are variables. You can do, for instance, `a[i]++` and (*p)++. It's not right to say "we can't modify [the value of] expression" though, since variables are also expressions, as are other lvalues. – Joshua Taylor Oct 09 '13 at 14:46
  • @JoshuaTaylor Defiantly Nice example Thanks! – Grijesh Chauhan Oct 09 '13 at 14:46
  • @JoshuaTaylor Note but `++(*p)++` this is lvalue error :) While `++*p++` is **NOT** – Grijesh Chauhan Oct 09 '13 at 14:53
  • Sure, but that's a precedence issue. What exactly is `++*p++` _doing_? What's incremented? What pointer is deferenced? What's incremented afterward? I'm not sure without testing, but I expect that since it compiles, it's doing (p = p+1), then doing `++(*p)`. – Joshua Taylor Oct 09 '13 at 14:58
  • @JoshuaTaylor `++*p++` is equivalent to `++*p;` `p++;` first value addressed by `p` is incremented then `p` itself incremented to make point to next location. Note parenthesized version of `++*p++` is: `++(*(p++));` – Grijesh Chauhan Oct 09 '13 at 15:00
  • Thanks for the explanation. Regardless of how it's parsed, it still drives home the point that the argument to the pre- and post-increment operators can be arbitrary lvalues, not just variables. – Joshua Taylor Oct 09 '13 at 15:06
  • @JoshuaTaylor The first link I given describes technically what is modifiable lvalue. – Grijesh Chauhan Oct 09 '13 at 15:18
  • @JoshuaTaylor As I had good discussion with you. I wanted to share a docs statement regarding your first comment. [read from here](http://supp.iar.com/Support/?note=82120) "The assignment requires a unary-expression on the left side. A cast-expression is not part of a unary-expression. As mentioned above, you can use a unary-operator before the cast-expression to get a unary-expression, such as `'*'`." – Grijesh Chauhan Oct 10 '13 at 04:14
5

y = x++ returns the value of the increment on the variable x.

y = (x + z)++ fails because (x + z) is NOT a variable.

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
opalenzuela
  • 3,139
  • 21
  • 41
4

It's illegal because (w+x) is not a legal left hand side value.

A left hand side value is a value that can be assigned to (I.E. a variable).

Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292
3

Post-increment requires an l-value, which w+x is not.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • I like to change it `post/prefix incremented and decrements` Also want to share a link: [Expression must be a modifiable lvalue error](http://supp.iar.com/Support/?note=82120) – Grijesh Chauhan Oct 09 '13 at 15:13
2

It is illegal because (x+y) is not a variable.

Consider

a++ is a = a + 1

(x+y)++ is what? (x+y) = (x+y) + 1?

Tun Zarni Kyaw
  • 2,099
  • 2
  • 21
  • 27
2

Post inctrement and decrement requires l-value (say a variable which comes on left side). w+x is not a variable. Incrementing w+x is like incrementig 3 + 4, which is illegal.

haccks
  • 104,019
  • 25
  • 176
  • 264