-3
#include <stdio.h>

int main()
{
    int a[3] = {1, 2, 3};
    int *b = a;
    int c = ++(++(*++b)); /* error: lvalue required as increment operand */
    printf("%d", c);

    return 0;
}

But the following is legal:

int c = *++b+1+1;

Why such a difference exists?

1MinLeft
  • 93
  • 7
  • @KamiKaze This is rather about `++b` not being a lvalue. – a3f Apr 13 '18 at 06:40
  • 2
    The result of the `++` operators is not a so-called _lvalue_. That is, the result is a value but not one you can modify. The result of the `*` operator is however always a lvalue. This is why `++*++b` is syntactically correct but `++++*++b` is not. – Lundin Apr 13 '18 at 06:44
  • Even if it had been legal, why would you like to write a longer and more complicated code, when there is a shorter (and legal) version? I also recommend `+ 2` over `+1+1`. – Bo Persson Apr 13 '18 at 14:54
  • @Bo In practical use, I will surely not do that; now I just want to understand the language, so I asked the question. – 1MinLeft Apr 13 '18 at 23:57

1 Answers1

-3

The following one int c = *++b+1+1; is legal because of

  • there are 3 operator namely *, ++ and + and check man 1 operator * and ++ having same precedence so check associativity which is R-->L
  • so 1st *++b is solved which is valid & it gives value 2
  • Now it looks like int c = 2+1+1 which is also valid C statement

And int c = ++(++(*++b)); is illegal because first *++b is performed which gives one value lets say 2(res) now you are doing ++res which results in an integer value 3 until this is fine, next when you do ++3 which is invalid because ++ is applicable on variable not on constants. So it gives lvalue error.

Achal
  • 11,821
  • 2
  • 15
  • 37