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?
- A. pc = &x;
- B. p = pc;
- C. pc = p;
- 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
- What is printed? c #define S(x) #x printf("%zu %zu", strlen(S("hi")), sizeof(S(ab)));
- What is printed? c int paths(int r, int c) { return (r == 0 || c == 0) ? 1 : paths(r - 1, c) + paths(r, c - 1); } int main(void) {…
- Assume 8-bit unsigned char. What is printed? c int n = 0; for (unsigned char c = 0; c < 300; c++) { n++; if (n > 1000) break; }…
- Assume IEEE-754 float (24-bit significand) and 32-bit int. What is printed? c int big = 16777217; / 2^24 + 1 / float f = big; printf("%d"…
- Assume 8-bit unsigned char. What is printed? c int i; unsigned char c; double d; d = c = i = 300 + 0.7; printf("%d %d %.1f", i, c, d);
- Why is memcmp(&a, &b, sizeof a) == 0 an unreliable way to test whether two struct variables a and b have equal members?