ProgrammingVery hardProduct company OA · GATE CS

Given char s[] = "ab"; and the declarations below, for which of p1 to p4 does the statement p = 'x'; compile?

Given char s[] = "ab"; and the declarations below, for which of p1 to p4 does the statement *p = 'x'; compile?
typedef char *PC;
const PC p1 = s;
PC const p2 = s;
const char *p3 = s;
char const *p4 = s;
  1. A. None of them
  2. B. p1 and p2 only
  3. C. p2 only
  4. D. p3 and p4 only

Correct answer: B. p1 and p2 only

A typedef is not a macro: const PC means 'constant PC', i.e. char *const, exactly like PC const. So p1 and p2 are constant pointers to modifiable char (*p = 'x' is fine, p++ is not). p3 and p4 are both pointers to const char. Reading const PC as 'const char *' is the trap.

More C Programming questions

Exams that ask this topic