ProgrammingHardHCL · Tech Mahindra pattern
What is printed? const qualifies what p points to, not p itself.
What is printed? `const` qualifies what `p` points to, not `p` itself.
int x = 5, y = 10;
const int *p = &x;
p = &y;
printf("%d", *p);- A. 10
- B. 5
- C. Compilation error: cannot reassign p
- D. Compilation error: cannot read *p
Correct answer: A. 10
const int *p means the pointed-to int cannot be modified through p (*p = ... would be an error), but p itself is an ordinary variable and can be repointed freely. p = &y is legal, so *p reads y = 10.
More C Programming questions
- What is printed? c int a = 10; int p = &a; int q = p; p = NULL; printf("%d", q);
- What is printed? c int f(int n) { if (n == 0) return 0; return n + f(n - 1); } int main(void) { printf("%d", f(4)); }
- What is printed? c int fib(int n) { if (n <= 1) return n; return fib(n - 1) + fib(n - 2); } int main(void) { printf("%d", fib(6)); }
- What is printed? c int f(int n) { if (n == 0) return 0; printf("%d ", n); if (n % 2 == 0) return f(n / 2); return f(n - 1); } int…
- How many TOTAL calls to fib (including the initial call) are made evaluating fib(4), given this naive recursive implementation? c int…
- What is printed? (recursion where the recursive call's result is deliberately ignored) c int calls = 0; int f(int n) { calls++; if (n <=…