1. Home
  2. Computer Science MCQs
  3. C Programming
  4. int a[3] = {10, 20, 30}; int *p = a; int x = *p++; what…

Added 30 Nov -0001

int a[3] = {10, 20, 30}; int *p = a; int x = *p++; what are x and the new value of *p?

  1. x is 11, because the increment applies to the pointed-to int rather than to the pointer itself
  2. x is 10 and *p is now 20, because postfix ++ binds tighter than * and yields the old pointer value Correct answer
  3. x is 10 and p still points at a[0], because the unary * is evaluated before the postfix increment
  4. x is 20, because the pointer is incremented before it is dereferenced and the old value is discarded

Correct answer

B. x is 10 and *p is now 20, because postfix ++ binds tighter than * and yields the old pointer value

17 views

MCQSEXAM

Explanation

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.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is B? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More C Programming MCQs

Practise all C Programming MCQs

Similar MCQs