ProgrammingHardProduct company OA

What is printed?

What is printed?
struct A { int v[3]; };
struct A make(void) { struct A a = {{1, 2, 3}}; return a; }
int main(void) {
    struct A x = make(), y = x;
    y.v[0] = 9;
    printf("%d %d", x.v[0], make().v[2]);
}
  1. A. Compilation error: arrays cannot be returned
  2. B. 1 3
  3. C. 9 3
  4. D. 1 9

Correct answer: B. 1 3

A function cannot return a bare array, but it can return a struct containing one, and struct assignment copies the whole array. y is an independent copy, so changing it leaves x.v[0] = 1. Member access on a returned struct, make().v[2], is fine.

More C Programming questions

Exams that ask this topic