Bird
Raised Fist0
Cprogramming~20 mins

Nested structures - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Nested Structures Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of nested struct member access
What is the output of this C code snippet?
C
typedef struct {
    int x;
    int y;
} Point;

typedef struct {
    Point topLeft;
    Point bottomRight;
} Rectangle;

#include <stdio.h>

int main() {
    Rectangle rect = {{1, 2}, {3, 4}};
    printf("%d\n", rect.bottomRight.x);
    return 0;
}
A2
B4
C3
D1
Attempts:
2 left
💡 Hint
Look at how the nested structs are initialized and accessed.
❓ Predict Output
intermediate
2:00remaining
Value of nested struct after assignment
What is the value of p2.y after this code runs?
C
typedef struct {
    int x;
    int y;
} Point;

#include <stdio.h>

int main() {
    Point p1 = {5, 10};
    Point p2;
    p2 = p1;
    p2.y = 20;
    printf("%d\n", p2.y);
    return 0;
}
A20
B5
C10
D0
Attempts:
2 left
💡 Hint
Remember that assigning structs copies all members.
❓ Predict Output
advanced
2:00remaining
Output of nested struct pointer access
What does this program print?
C
typedef struct {
    int id;
    struct {
        int day;
        int month;
        int year;
    } date;
} Event;

#include <stdio.h>

int main() {
    Event e = {101, {15, 8, 2023}};
    Event *ptr = &e;
    printf("%d-%d-%d\n", ptr->date.day, ptr->date.month, ptr->date.year);
    return 0;
}
A101-15-8
B15-8-2023
C8-15-2023
D2023-8-15
Attempts:
2 left
💡 Hint
Use the arrow operator to access nested struct members through a pointer.
❓ Predict Output
advanced
2:00remaining
Result of modifying nested struct via pointer
What is the output of this program?
C
typedef struct {
    int x;
    int y;
} Point;

typedef struct {
    Point center;
    int radius;
} Circle;

#include <stdio.h>

int main() {
    Circle c = {{0, 0}, 5};
    Circle *pc = &c;
    pc->center.x = 10;
    printf("%d\n", c.center.x);
    return 0;
}
A10
B0
CCompilation error
D5
Attempts:
2 left
💡 Hint
Changing a nested struct member via pointer affects the original struct.
❓ Predict Output
expert
3:00remaining
Output of complex nested struct initialization and access
What is the output of this program?
C
typedef struct {
    int day;
    int month;
    int year;
} Date;

typedef struct {
    char name[20];
    Date birthdate;
} Person;

#include <stdio.h>

int main() {
    Person people[2] = {
        {"Alice", {1, 1, 2000}},
        {"Bob", {31, 12, 1999}}
    };
    printf("%s was born on %d/%d/%d\n", people[1].name, people[1].birthdate.day, people[1].birthdate.month, people[1].birthdate.year);
    return 0;
}
AAlice was born on 31/12/1999
BAlice was born on 1/1/2000
CBob was born on 1/1/2000
DBob was born on 31/12/1999
Attempts:
2 left
💡 Hint
Check the array index and nested struct initialization carefully.

Practice

(1/5)
1. What is a nested structure in C programming?
easy
A. A structure that contains another structure as a member
B. A structure with only one member
C. A structure declared inside a function
D. A structure that uses pointers only

Solution

  1. Step 1: Understand structure composition

    A nested structure means one structure is included inside another as a member.
  2. Step 2: Rule out distractors

    The other options describe unrelated concepts: a structure using pointers only, with only one member, or declared inside a function.
  3. Final Answer:

    A structure that contains another structure as a member -> Option A
  4. Quick Check:

    Nested structure = structure inside structure [OK]
Hint: Nested means one structure inside another [OK]
Common Mistakes:
  • Thinking nested means pointer usage
  • Confusing nested with single-member structures
  • Assuming nested means declared inside functions
2. Which of the following is the correct syntax to access a member of a nested structure in C?
easy
A. outer.inner->member
B. outer->inner->member
C. outer.inner.member
D. outer->inner.member

Solution

  1. Step 1: Identify access operator for nested structures

    When using nested structures (not pointers), use dot notation for each level: outer.inner.member.
  2. Step 2: Understand pointer vs non-pointer access

    Arrow (->) is used only when accessing members through pointers, which is not stated here.
  3. Final Answer:

    outer.inner.member -> Option C
  4. Quick Check:

    Use dot for nested struct members [OK]
Hint: Use dot notation for nested struct members [OK]
Common Mistakes:
  • Using arrow (->) when not using pointers
  • Mixing dot and arrow incorrectly
  • Confusing member access syntax
3. What is the output of the following C code?
typedef struct {
    int day;
    int month;
    int year;
} Date;

typedef struct {
    char name[20];
    Date birthday;
} Person;

int main() {
    Person p = {"Alice", {15, 8, 1990}};
    printf("%d-%d-%d", p.birthday.day, p.birthday.month, p.birthday.year);
    return 0;
}
medium
A. 15-8-1990
B. Alice-15-8
C. 1990-8-15
D. Compilation error

Solution

  1. Step 1: Understand structure initialization

    Person p is initialized with name "Alice" and birthday as Date {15, 8, 1990}.
  2. Step 2: Trace printf output

    printf prints p.birthday.day (15), p.birthday.month (8), p.birthday.year (1990) separated by dashes.
  3. Final Answer:

    15-8-1990 -> Option A
  4. Quick Check:

    Print nested struct members correctly [OK]
Hint: Access nested members with dot notation [OK]
Common Mistakes:
  • Printing name instead of date
  • Mixing order of day, month, year
  • Forgetting to initialize nested struct
4. Identify the error in this nested structure code snippet:
struct Address {
    char city[20];
    int zip;
};

struct Employee {
    char name[30];
    struct Address addr;
};

int main() {
    struct Employee emp;
    emp.addr->zip = 12345;
    return 0;
}
medium
A. Missing semicolon after struct definitions
B. Using '->' operator on a non-pointer structure variable
C. Incorrect declaration of nested structure
D. Cannot assign integer to zip member

Solution

  1. Step 1: Check member access syntax

    emp.addr is a structure variable, not a pointer, so '->' operator is invalid here.
  2. Step 2: Correct operator usage

    Use dot operator '.' to access zip: emp.addr.zip = 12345;
  3. Final Answer:

    Using '->' operator on a non-pointer structure variable -> Option B
  4. Quick Check:

    Use '.' for struct variables, '->' for pointers [OK]
Hint: Use '.' for struct, '->' only for pointers [OK]
Common Mistakes:
  • Using '->' on struct variables
  • Confusing pointer and non-pointer access
  • Ignoring compiler error messages
5. Given the following nested structures, how would you write a function in C that takes a Person and returns the birth year?
typedef struct {
    int day;
    int month;
    int year;
} Date;

typedef struct {
    char name[50];
    Date birthday;
} Person;

// Function prototype here
Which of the following is the correct function definition?
hard
A. int getBirthYear(Person *p) { return p.birthday->year; }
B. int getBirthYear(Person *p) { return p->birthday.year; }
C. int getBirthYear(Person p) { return p.year; }
D. int getBirthYear(Person p) { return p.birthday.year; }

Solution

  1. Step 1: Understand parameter passing

    The function takes a Person by value, so use dot notation to access members.
  2. Step 2: Access nested member correctly

    Access birthday.year as p.birthday.year since p is not a pointer.
  3. Final Answer:

    int getBirthYear(Person p) { return p.birthday.year; } -> Option D
  4. Quick Check:

    Use dot for struct value parameters [OK]
Hint: Use dot for struct, arrow for pointer parameters [OK]
Common Mistakes:
  • Using arrow (->) on non-pointer parameter
  • Accessing wrong member name
  • Confusing pointer and value parameter syntax
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard