ProgrammingModerateProduct-company MCQ pattern

Given int x = 1, y = 2; const int *p = &x; int *const q = &x; which pair of statements both fail to compile?

  1. A. p = &y; and *q = 5;
  2. B. *p = 5; and q = &y;
  3. C. *p = 5; and *q = 5;
  4. D. p = &y; and q = &y;

Correct answer: B. *p = 5; and q = &y;

Read right to left: p is a pointer to const int, so the pointee cannot be changed through p (*p = 5 fails) but p can be re-pointed. q is a const pointer to int, so q cannot be re-pointed (q = &y fails) but *q can be changed.

More C Programming questions

Exams that ask this topic