ProgrammingHardIBM · Deloitte pattern

Assume a 64-bit LP64 ABI. What is printed?

Assume a 64-bit LP64 ABI. What is printed?
void process(int *data, int len) {
    for (int i = 0; i < len; i++) data[i] *= 2;
}
int main(void) {
    int arr[4] = {1, 2, 3, 4};
    process(arr, 4);
    printf("%d %d %d %d", arr[0], arr[1], arr[2], arr[3]);
}
  1. A. 1 2 3 4
  2. B. 2 4 6 8
  3. C. Undefined behaviour: process receives a decayed pointer, not the original array
  4. D. Compile-time error: arr cannot be passed to a function expecting int*

Correct answer: B. 2 4 6 8

Passing arr decays it to a plain int* pointing at the same memory; process() receives this pointer and modifies the underlying array elements directly through it (no copy is made of the array's contents). After doubling each, arr becomes 2 4 6 8 — the caller sees the mutation because arrays are always effectively passed by reference to the underlying data in C.

More C Programming questions

Exams that ask this topic