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;
- A. None of them
- B. p1 and p2 only
- C. p2 only
- 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
- Assume 32-bit unsigned int. What is printed? c unsigned int u = 1u << 31; printf("%u %u", u >> 31, (u >> 30) | (u >> 28 & 1));
- What is printed? c int x = 5, s = 0; switch (x) { default: s += 1; case 1: s += 10; case 2: s += 100; break; case 3: s += 1000; }…
- What is f in the declaration below? c int ((f)(int))[3];
- What is printed? c char s[10] = "abc"; char t[] = "xyz"; strncat(s, t, 2); printf("%s %zu %d", s, strlen(s), strncmp(s, "abcx", 4) == 0);
- What is printed? c char s[] = {"alpha", "beta", "gamma", "delta"}; char p = s; p += 2; printf("%c ", p); printf("%c ", ((p - 1) + 2))…
- What is printed? c int c = 0; int g(int n) { c++; if (n <= 2) return 1; return g(n - 1) + g(n - 2) + g(n - 3); } int main(void) { int r =…