-4

What is difference between ++*var++ and ++var++ ? why ++*var++ is working while ++var++ results with lvalue required error in C?

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • You have neglected to mention the type of `var` – jaggedSpire Jun 28 '18 at 14:53
  • I do not see any relevance of the proposed "duplicate" question – Ctx Jun 28 '18 at 14:54
  • @Ctx, this duplicate is more appropriate: https://stackoverflow.com/questions/8208021/how-to-increment-a-pointer-address-and-pointers-value – Umaiki Jun 28 '18 at 14:56
  • @Umaiki Yes, maybe, but still doesn't qualify as a full duplicate in my eyes. Lets reopen, Bathshebas answer is fine – Ctx Jun 28 '18 at 14:57
  • 1
    Guys, this has little to do with sequencing points. There are two objects being changed here, not one twice. I've reopened. – Bathsheba Jun 28 '18 at 14:59
  • 4
    @Senthilkumar Where did these two expressions come from? Did you find them in an actual program? Were they part of a class assignment? Or are you experimenting with all permutations and combinations of prefix `++`, postfix `++`, and `*`, in order to make sure you understand what they all do? If you're experimenting, I must caution you: many of the combinations (such as the two you've asked about) are so meaningless that experimenting with them is not likely to teach you anything useful, it may only confuse you further. – Steve Summit Jun 28 '18 at 15:09
  • @Senthilkumar *why `++*var++` is working* If you think that's working, you would know what it does. If you found that code somewhere, get rid of it. Delete it from your hard drive. Then take that hard drive out, melt it down into a small ball of metal, and start a GoFundMe effort to have NASA or some other space agency launch the remains of your hard drive into the Sun just to put it out of the misery it must be feeling over holding such code. – Andrew Henle Jun 28 '18 at 16:35
  • 1
    Please don't ever write code like that. – Jesper Juhl Jun 28 '18 at 17:24

2 Answers2

5

++var++ is grouped as ++(var++) and that results in a compilation failure since var++ is not an lvalue. (Informally speaking this means you can't put it on the left hand side of an assignment).

++*var++ is grouped as ++(*(var++)) which means increment the pointer var by 1 using pointer arithmetic, then increase the dereferenced value of the initial value of var by 1.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • 2
    Supplemental reading: [What are rvalues, lvalues, xvalues, glvalues, and prvalues?](https://stackoverflow.com/questions/3601602/what-are-rvalues-lvalues-xvalues-glvalues-and-prvalues) – user4581301 Jun 28 '18 at 14:54
5

Even though I was just advising against this in a comment, it may be useful to look at all possibilities. For a pointer variable p, we have:

  1. ++p             increment p, yield new value
  2. p++             increment p, yield old value
  3. *++p           increment p, access what it newly points to
  4. *p++           increment p, access what it used to point to
  5. ++*p           increment what p points to, yield new value
  6. (*p)++        increment what p points to, yield old value
  7. ++*++p        increment p, increment what it newly points to, yield new value
  8. (*++p)++     increment p, increment what it newly points to, yield old value
  9. ++(*p++)     increment p, increment what it used to point to, yield new value
  10. (*p++)++     increment p, increment what it used to point to, yield old value
  11. ++p++          meaningless

Cases 9 and 11 are the ones you were asking about. In case 9, I've added explicit parenthesis to make it more clear what's going on. I guess it also works if you leave them out, and say ++*p++, but this makes my head explode, so I'd prefer not to think about it any more.

Steve Summit
  • 45,437
  • 7
  • 70
  • 103