-1

The expressions x++; and x*2; are reported as illegal. Compiler generates error. However, in case of x+1; there's no error reported.

Can anyone help me understand why the particular expressions are illegal? Or, in other way, why the addition is legal?

Code:

#include <stdio.h>
int main(void)
{ 
   int x[]={1,2,3,4,5};
   x++;
   x+1; 
   x*2;
}    
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
sociopath
  • 79
  • 1
  • 6

3 Answers3

4

Actually, you need to understand how and when the operators are allowed. Let's see one by one.

Quoting from C11,

  • ++, Postfix increment, chapter §6.5.2.3

    The operand of the postfix increment or decrement operator shall have atomic, qualified, or unqualified real or pointer type, and shall be a modifiable lvalue.

  • +, Additive operator, chapter §6.5.6

    For addition, either both operands shall have arithmetic type, or one operand shall be a pointer to a complete object type and the other shall have integer type. (Incrementing is equivalent to adding 1.)

  • *, Multiplicative operator, chapter §6.5.5

    Each of the operands shall have arithmetic type. [...]

Now, in your usage

  • x++; --> x is not a modifiable lvalue, it is of "array type". This causes error.
  • x+1; --> Satisfies the requirement see notes for array decay, hence valid.
  • x*2; --> x is not arithmetic type (its'a pointer type), hence invalid.

Additional notes: (all emphasis mine)

  • "Modifiable lvalue:"

    Quoting chapter §6.3.2.1,

    An lvalue is an expression (with an object type other than void) that potentially designates an object; [...] A modifiable lvalue is an lvalue that does not have array type, does not have an incomplete type, does not have a const-qualified type, and if it is a structure or union, does not have any member (including, recursively, any member or element of all contained aggregates or unions) with a const-qualified type.

  • "Arithmetic Type:"

    Quoting §6.2.5,

    Integer and floating types are collectively called arithmetic types. [...]

  • "Array decay:"

    Quoting §6.3.2.1,

    Except when it is the operand of the sizeof operator, the _Alignof operator, or the unary & operator, or is a string literal used to initialize an array, an expression that has type ‘‘array of type’’ is converted to an expression with type ‘‘pointer to type’’ that points to the initial element of the array object and is not an lvalue. [...]

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
4

Among these three statements

x++;
x+1; 
x*2;

there is only two statements are illegal. This statement

x+1; 

is correct. In this case in the expression the array designator is converted to pointer to its first element.

Multiplicative operators are not defined for pointers. So this statement

x*2;

is illegal.

Arrays are non-modifiable lvalues. You may not change an array designator. So this statement

x++;

is illegal.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

Here, x is an array and you cannot modify the address of an array. It is illegal in C.

GCC compiler gives an error :

 In function 'main':
5:5: error: lvalue required as increment operand
    x++;
     ^
7:5: error: invalid operands to binary * (have 'int *' and 'int')
    x*2;
msc
  • 33,420
  • 29
  • 119
  • 214