Search code examples
coperatorsoperator-precedence

Why *p++ dereferences a pointer first and then increments pointer adress?


The C operator precedence chart looks like this: C operator precedence

Why is post increment/decrement first on the list but *p++ results in dereferencing a pointer first and then incrementing adress it points to?


Solution

  • *p++ parses as *(p++).

    the value of p++ is p (and the side effect is incrementing p), so the value of *p++ is the same value as *p.

    Except for the side-effect, *p and *p++ are identical.

    The exact same thing happens with integer:

    int n = 6;
    printf("%d\n", 7 * n);
    printf("%d\n", 7 * n++); // except for the side-effect same value as above