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);
}
  1. A. 40 40
  2. B. 8 8
  3. C. 40 8 (inside modify, arr is really a pointer parameter, not an array)
  4. 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

Exams that ask this topic