ProgrammingHard
struct Box { int w; int h; }; struct Box boxes[2] = {{2, 3}, {4, 5}}; int area = 0; for (struct Box *p = boxes; p < boxes + 2; p++) area += p->w * p->h; printf("%d", area); What is printed?
- A. 14
- B. 26
- C. 20
- D. 12
Correct answer: B. 26
p walks the array of structs, so the loop adds 2 x 3 = 6 and 4 x 5 = 20, giving 26. 14 adds all four fields instead of multiplying, and 20 keeps only the last box's area.
More C Programming questions
- struct Q { int v; }; struct Q q[3] = {{5}, {1}, {3}}; struct Q lo = q; for (int i = 1; i < 3; i++) if (q[i].v < lo->v) lo = &q[i]…
- struct T { int a; double d; }; printf("%d", (int)(sizeof(struct T) == 16)); What is printed with 4-byte int and 8-byte double (natural…
- struct F { char c; double d; }; printf("%zu", sizeof(struct F)); What is printed with natural alignment and 8-byte double?
- struct G { int x; char y[3]; }; printf("%zu", sizeof(struct G)); What is printed with 4-byte int and natural alignment?
- struct H { short s; char c; }; printf("%zu", sizeof(struct H)); What is printed with 2-byte short and natural alignment?
- union U { int i; char c[6]; }; printf("%zu", sizeof(union U)); What is printed with 4-byte int and 1-byte char?