ProgrammingHardTCS NQT · Infosys pattern
What is printed?
What is printed?
void modify(int arr[10]) {
printf("%lu ", sizeof(arr));
}
int main(void) {
int a[10];
printf("%lu ", sizeof(a));
modify(a);
}- A. 40 40
- B. 8 8
- C. 40 8 (inside modify, arr is really a pointer parameter, not an array)
- D. 40 4
Correct answer: C. 40 8 (inside modify, arr is really a pointer parameter, not an array)
In main, a is a real array of 10 ints: sizeof(a) = 40. A function parameter written as int arr[10] is adjusted by the compiler to int *arr, so sizeof(arr) inside modify is just the size of one pointer (8 on a 64-bit ABI) — the array-size information is lost.
More C Programming questions
- What is printed? const qualifies what p points to, not p itself. c int x = 5, y = 10; const int p = &x; p = &y; printf("%d", p);
- 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…