int a[3] = {10, 20, 30}; int *p = a; int x = *p++; what are x and the new value of *p?
Correct answer
B. x is 10 and *p is now 20, because postfix ++ binds tighter than * and yields the old pointer value
17 views
B. x is 10 and *p is now 20, because postfix ++ binds tighter than * and yields the old pointer value
17 views
Operator precedence places postfix ++ above unary *, so *p++ parses as *(p++) and the dereference uses the pre-increment pointer, while the increment takes effect before the next sequence point. Hence x receives 10 and p then points at a[1]. Writing (*p)++ would increment the stored value instead and leave the pointer unchanged.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is B? Ask here and get an explanation.