ProgrammingHardTCS NQT · Infosys pattern

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

Assume a 64-bit LP64 ABI. What is printed?
void modify(int *arr[], int n) {
    for (int i = 0; i < n; i++) *arr[i] += 100;
}
int main(void) {
    int a = 1, b = 2, c = 3;
    int *ptrs[3] = {&a, &b, &c};
    modify(ptrs, 3);
    printf("%d %d %d", a, b, c);
}
  1. A. Compilation error: int *arr[] is not a valid parameter type
  2. B. 1 2 3
  3. C. 101 102 103
  4. D. 100 100 100

Correct answer: C. 101 102 103

ptrs is an array of pointers to distinct int variables, decaying to int** when passed. modify dereferences each element (*arr[i]) to reach the pointed-to int and adds 100 to it in place, mutating a, b and c through their addresses directly: 101 102 103.

More C Programming questions

Exams that ask this topic