ProgrammingHardAccenture · Cognizant pattern

What is printed?

What is printed?
void modify(int *arr, int n) {
    for (int i = 0; i < n; i++) arr[i] *= 2;
}
int main() {
    int a[] = {1, 2, 3};
    int *p = a;
    modify(p, 3);
    printf("%d %d %d", a[0], a[1], a[2]);
}
  1. A. Compilation error: array decays, modify cannot change caller's array
  2. B. 1 2 3
  3. C. 2 4 6
  4. D. 0 0 0

Correct answer: C. 2 4 6

Arrays decay to pointers when passed to functions, so modify receives the same address as a. Writing through arr[i] inside modify directly mutates a's own storage in main, doubling each element.

More C Programming questions

Exams that ask this topic