-2

I made a silly mistake of doing *p++; thinking that p would be dereferenced first and then the value pointed by p would get incremented. So I found that this is more akin to:

*(p++);

Because the postfix ++ increment operator has higher precedence than the * dereference operator.

But now that I'm thinking in terms of operator precedence, I find that postfix++ is higher than for example the equality == operator, but in:

int a = 0;
if (0 == a++) // Condition is true

And the same goes for ! logical NOT:

int a = 0;
if (!a++) // Condition is true, a is incremented after the condition check
          // even if it's higher precedence than !

So there must be more to it from what I've seen.

I've heard about the right-to-left and clockwise/spiral rule, but every time I've tried to grasp it it escaped my understanding.

I don't want a full explanation on the evaluation order, as I can struggle through that with my own reading. But am I at least right that it's not just a matter of operator precedence? For example the answer that *p++ is evaluated the way it is because the postfix++ operator is of higher precedence is only half right or part of the answer?

Link to answer

Zebrafish
  • 11,682
  • 3
  • 43
  • 119
  • 2
    This might help: https://stackoverflow.com/questions/9255147/does-p-increment-after-dereferencing – mfnx Mar 14 '18 at 07:22
  • 4
    Postfix `++` increments the variable, but returns the old value. That's the whole point of postfix `++`. – melpomene Mar 14 '18 at 07:23
  • 2
    "Precedence" in C and C++ is lexical. "Executed before" is not something you should consult a precedence table for. – StoryTeller - Unslander Monica Mar 14 '18 at 07:24
  • 3
    "Operator precedence" and "order of evaluation" are completely different things. – n. m. could be an AI Mar 14 '18 at 07:25
  • As @n.m. said, precedence and order are different things. Precedence defines that in `f(1)+g(2)*h(7)` the value returned from `f()` call will be added to a product of values of `g()` and `h()`, while order of evaluation defines (or sometimes does not) which of `f()`, `g()` and `h()` can be executed first and which last. – CiaPan Mar 14 '18 at 07:37
  • It's called *post increment* (`p++`) because the increment happens (notionally) *after* the value is taken. This is as opposed to *pre increment* (`++p`) where the increment happens *before* the value is taken. – Galik Mar 14 '18 at 07:38
  • Then that answer when it says that it's evaluated that way because postfix++ operator is higher precedence than the dereference operator is only half right then, that's what I'm asking. As precedence isn't the sole factor. – Zebrafish Mar 14 '18 at 07:56
  • @Zebra - It's a good idea to avoid mixing increments/decrements with other sub-expressions. Otherwise you sooner or later end up [here](https://stackoverflow.com/questions/949433/why-are-these-constructs-using-undefined-behavior-in-c). `if (!a++)` is just *hard to read* code for no particular reason. "Being too smart is not smart"(TM). – Bo Persson Mar 14 '18 at 09:40

2 Answers2

2

Your claim, Condition is true, a is incremented after the condition check even if it's higher precedence than !, is wrong.
It is not necessary that a will be incremented after the condition check. It can happen anytime. The only certainty is that in expression !a++, old value of a will be used for ! operator.
Operator precedence will help in grouping operands. Operand in !a++ will be grouped as ! (a++). This grouping doesn't mean that a will be incremented before ! operator is applied to the result fo a++.

haccks
  • 104,019
  • 25
  • 176
  • 264
1

p++ means increment p and return it's original value, it has to be evaluated before any other operator that uses its value. If you think of increment as a function it might be easier to understand:

int inc(int& v)
{
  int value = v;
  v++;
  return value;
}

int a = 0;
if (inc(a) == 0)
Alan Birtles
  • 32,622
  • 4
  • 31
  • 60