ProgrammingVery hardProduct company OA · GATE CS

Given `int x; const int *pc; int *p; int *const cp = &x;`, which assignment is a constraint violation that the compiler must diagnose?

  1. A. pc = &x;
  2. B. p = pc;
  3. C. pc = p;
  4. D. *cp = 5;

Correct answer: B. p = pc;

Assigning a const int * to an int * would discard the const qualifier and let you write through p to something promised read-only, so it needs a diagnostic. Adding const (pc = p, pc = &x) is always allowed, and cp is a constant pointer to a non-const int, so *cp = 5 is fine.

More C Programming questions

Exams that ask this topic