7

When I read the TCPL by K&R, I just couldn't understand two expressions:

*p++ = val;  /*push val onto stack */

Here is my idea:

  • dereference and postfix has the same precedence, and associativity is right to left,so

    *p++ = val maybe the same with *(p++) = val, because the pointer usually is the next position to the top , so in this code, p increase 1 first because of the parenthesis, so the p is the two units above the current top ,but not the one unit above the current top ,where the val should be!!! Thx

skyline09
  • 129
  • 1
  • 6
  • 1
    Post-fix operator has higher precedence than dereference `*`, so it is exec first: http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Operator_precedence And `p++` will return the current value for dereference, not the incremented value. – nhahtdh Mar 11 '13 at 18:05
  • in short, `*p++ = val` means: 1) set the variable referenced by `p` to the value `val` 2) then increment the pointer `p` – varnie Mar 11 '13 at 18:08

3 Answers3

10

The prefix increment/decrement and dereference operators are equal precedence, but the postfix operator is higher, so *p++ is the same as *(p++), which is like writing *p = val; p++;

If you wrote (*p)++ = val, it wouldn't compile, as you'd be trying to assign a value to a number.

teppic
  • 8,039
  • 2
  • 24
  • 37
  • But the p++ maybe exec first because of the parenthesis,so the p++ should before the dereference. Isn't it? – skyline09 Mar 11 '13 at 18:21
  • @wwwjieo0, the `++` *does* go first. But since it's a *post-increment* operator, its return value is `p` *before* incrementing. – Carl Norum Mar 11 '13 at 18:22
  • @CarlNorum I see, 3 things happend in order: 1.dereference 2.copy assignment 3.increase. Thx – skyline09 Mar 11 '13 at 18:28
  • @wwwjieo0: `p` doesn't get incremented until "later" - that means at the latest before the next statement, but not within the same expression like this. – teppic Mar 11 '13 at 18:34
4

Precedence and Associativity of Operators in K&R, table 2-1, pg 53, isn't as granular and complete as more recent table in Stroustrup, tC++PL,Sed, sec 6.2 Operator summary, p120-121.

C++ operator precedence Agnew's answer is excellent.

he points out association is indeed R->L for unary operators and that for *(p++),

  1. first p++ evaluates, but the previous p value is returned
  2. then *p is evaluated with this previous p value and the assignment occurs
  3. then the statement ends and p++ post increment value is now active, ie pointer p is now bumped.
Community
  • 1
  • 1
kwestphal
  • 41
  • 1
1

Precedence of operators is an order of their interpretation by compiler, not the order of their execution.

Operator precedence actually means "where to put parentheses". Hence you are correct that *p++ is the same as *(p++). But now we need to understand what is *(p++). It means taking *p and then increasing p++, because of post-fixed operation.

So, in short, you just mixed order of interpretation by compiler (which is determined by parentheses or precedence) and order of execution (which is determined by post- or pre-fixed definition).

AlexL
  • 246
  • 1
  • 2
  • 9