ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
/* 4-byte int */
struct V { int a[3]; };
void f(struct V v) { v.a[0] = 9; }
void g(int a[3])   { a[0] = 9; }
int main(void) {
    struct V v = {{1, 2, 3}};
    int w[3] = {1, 2, 3};
    f(v);
    g(w);
    printf("%d %d %zu", v.a[0], w[0], sizeof(struct V));
}
  1. A. 1 9 12
  2. B. 9 9 12
  3. C. 1 1 12
  4. D. 1 9 8

Correct answer: A. 1 9 12

A struct is passed by value: f receives a full copy, including the array inside it, so the caller's v.a[0] stays 1. A bare array parameter is really a pointer (int a[3] means int *a), so g writes into the caller's w. Wrapping an array in a struct is the C idiom for copying it by value; sizeof(struct V) is 12.

More C Programming questions

Exams that ask this topic