🇯🇵 日本語 | 🇺🇸 English

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 becomes an independent copy of a, so changing b.x doesn't 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 shorthand for (*ptr).y. Use the arrow operator to access members through 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".

Question 5 — Defining with typedef

typedef struct {
    int x;
    int y;
} Point;

Point p = {3, 4};
printf("%d\n", p.x + p.y);

What does this print?

Compile error
7
12
34
Explanation: typedef struct { ... } Point; gives the struct the alias Point.
  • From then on, you can write Point p; without the struct keyword
  • p.x = 3, p.y = 4
  • 3 + 4 = 7
typedef makes code easier to read and is common in larger projects.

Question 6 — Nested structs

struct Point { int x; int y; };
struct Line {
    struct Point start;
    struct Point end;
};
struct Line l = {{0, 0}, {3, 4}};
printf("%d\n", l.end.x);

What does this print?

0
3
4
Compile error
Explanation: Members of nested structs are reached through chained dots.
  • l.end is {3, 4} (a Point struct)
  • l.end.x = 3, l.end.y = 4
Nesting structs is a standard way to model more complex types.

Question 7 — Struct size and padding

// Typical 64-bit environment (int=4, char=1, alignment=4)
struct S {
    char c;
    int i;
};
printf("%lu\n", sizeof(struct S));

What is the most likely output?

5 (1 + 4)
8 (padding inserted)
4
2
Explanation: A struct's size isn't simply the sum of its members — padding is inserted to satisfy alignment requirements.
  • char c (1 byte) + 3 bytes of padding + int i (4 bytes) = 8 bytes
  • int typically has to sit on a 4-byte boundary, so padding is inserted after c
Ordering members from largest type to smallest reduces padding and improves memory efficiency.

Question 8 — Passing a struct by value

struct Point { int x; int y; };

void reset(struct Point p) {
    p.x = 0;
    p.y = 0;
}
int main(void) {
    struct Point pt = {5, 10};
    reset(pt);
    printf("%d %d\n", pt.x, pt.y);
    return 0;
}

What does this print?

0 0
5 10
0 10
Compile error
Explanation: Structs are also passed to functions by value (as a copy).
  • Inside reset, p is a copy of pt
  • Changing the copy has no effect on the caller's pt
  • So pt stays as 5 10
To modify the caller's struct, pass by pointer (void reset(struct Point *p)) and use p->x = 0;. Passing by pointer is also a good way to avoid copy costs on large structs.

Question 9 — Struct initialization

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

What does this print?

5 5 5
5 0 0
5 (indeterminate) (indeterminate)
Compile error
Explanation: When a struct initializer only covers some members, the remaining members are zero-filled (just like with arrays).
  • p.x = 5 (explicit)
  • p.y and p.z are omitted, so both default to 0
Writing struct Point p = {0}; zero-initializes every member. With C99 designated initializers, you can also initialize only specific members: {.x = 5}.

Result

Answer all the questions to see your score.
Home