ProgrammingVery hardProduct company OA
Which of the numbered statements violate a constraint (i.e. must be rejected by the compiler)?
Which of the numbered statements violate a constraint (i.e. must be rejected by the compiler)?
typedef char *str_t; #define STR_M char * char buf[] = "abc", alt[] = "xyz"; const str_t p = buf; const STR_M q = buf; /* 1 */ p[0] = 'A'; /* 2 */ p = alt; /* 3 */ q[0] = 'B'; /* 4 */ q = alt;
- A. 1 and 3 only
- B. 2 and 4 only
- C. 1, 2, 3 and 4
- D. 2 and 3 only
Correct answer: D. 2 and 3 only
A typedef is a real type name, so const str_t makes the POINTER itself const: char *const p. Writing through it (1) is fine, re-seating it (2) is an error. The macro is plain text: const STR_M q becomes const char *q, a modifiable pointer to const chars, so (3) is an error and (4) is fine. '1 and 3' reads both declarations as const char *, which is exactly the trap of mixing typedef and #define.
More C Programming questions
- What is printed? c int g = 1; int f(int n) { static int s = 10; int g = n; s += g; { extern int g; g += s; } return s; } int main(void) {…
- What is printed? c int i = 0, j = 5, k = 0; int r = (i++ || j++) && (k++, i + j > 1) ? i++ : (j += 2, k); printf("%d %d %d %d", r, i, j, k);
- What is printed? c const char w[] = {"heap", "stack", "queue", "trie"}; const char p = w; printf("%c", ++p); printf("%c", ++p)…
- Assume 8-bit unsigned char. What is printed? c unsigned int count = 0; for (unsigned char i = 250; i != 4; i += 3) count++; printf("%u"…
- Assume 32-bit unsigned int and 64-bit unsigned long long. What is printed? c unsigned int a = 0x80000001u; unsigned int b = a 2; int c =…
- Assume a 64-bit LP64 ABI (4-byte int, 8-byte pointers and double, natural alignment). What is printed? c char s[] = "a0bc"; char p = s…