🇯🇵 日本語 | 🇺🇸 English
Advertisement

Quiz (Structs)

Test your understanding of struct initialization, assignment, the arrow operator, and arrays of structs.

Question 1 — Struct basics

struct Point { int x; int y; };
struct Point p = {3, 7};
printf("(%d, %d)\n", p.x, p.y);

What does this print?

(3, 7)
(7, 3)
Compile error
(0, 0)
Explanation: Struct members are initialized in declaration order: p.x=3, p.y=7.

Question 2 — Struct assignment

struct Point a = {1, 2};
struct Point b;
b = a;
b.x = 10;
printf("a.x=%d, b.x=%d\n", a.x, b.x);

What does this print?

a.x=10, b.x=10
a.x=1, b.x=10
Compile error
a.x=1, b.x=1
Explanation: Struct assignment is a value copy. b is an independent copy of a, so changing b.x does not affect a.x.

Question 3 — Arrow operator

struct Point p = {5, 8};
struct Point *ptr = &p;
printf("%d\n", ptr->y);

What does this print?

5
8
Compile error
An address
Explanation: ptr->y is equivalent to (*ptr).y. Use the arrow operator to access members via a pointer. p.y = 8.

Question 4 — Array of structs

struct Student { char name[20]; int score; };
struct Student s[3] = {
    {"Taro", 80}, {"Hanako", 95}, {"Jiro", 70}
};
int max = 0;
for (int i = 0; i < 3; i++) {
    if (s[i].score > s[max].score) max = i;
}
printf("%s\n", s[max].name);

What does this print?

Taro
Hanako
Jiro
Compile error
Explanation: The loop finds the student with the highest score. s[1].score=95 is the highest, so max=1 and s[1].name = "Hanako".

Result

Please answer the questions
Home