Nested Structs
In addition to the basic primitive data types such as int or char, arrays and pointers, you can use other structures as members within your structure.
Several methods can be used:
- Declare and define the
NestedStruct
structure inside theOuterStruct
structure (embedded nested struct);
struct OuterStruct
{
<fields>;
struct NestedStruct
{
<fields>
}nested;
};
- Declare the
NestedStruct
structure separately from theOuterStruct
structure, create an instance of theNestedStruct
structure inside theOuterStruct
structure (separate nested struct).
struct NestedStruct
{
<fields>;
};
struct OuterStruct
{
<fields>
struct NestedStruct nested;
};
The difference is that when a nested structure is declared separately, that structure can be used in many other structures.
If an instance of a nested structure was created outside of an external structure, then the structure will not be nested.
struct OuterStruct {
int outer_field;
struct NestedStruct {
int nested_field;
};
};
struct OuterStruct outer_struct;
struct NestedStruct inner_struct; // not nested struct
You can access the fields of a nested structure directly through the "." operator or through a pointer and the -> operator.
An example of using nested structures:
main.c
123456789101112131415161718192021222324252627#include <stdio.h> // structure for address struct Address { char street[50]; char city[50]; char index[10]; }; // structure for person struct Person { char name[50]; int age; struct Address address; // nested structure }; int main() { // initialization of Person structure struct Person person = { "Sherlock Holmes", 27, {"Baker Street", "London", "221B"} }; // display information about the person and his address printf("Name: %s\n", person.name); printf("Age: %d\n", person.age); printf("Address: %s, %s, %s\n", person.address.index, person.address.street, person.address.city); return 0; }
Swipe to start coding
- Define a nested structure
education
; - Create an instance of the
Student
structure; - Display the student's name and age;
- Display information about the student’s education.
Oplossing
Bedankt voor je feedback!
single
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.
Vat dit hoofdstuk samen
Explain code
Explain why doesn't solve task
Awesome!
Completion rate improved to 4.17
Nested Structs
Veeg om het menu te tonen
In addition to the basic primitive data types such as int or char, arrays and pointers, you can use other structures as members within your structure.
Several methods can be used:
- Declare and define the
NestedStruct
structure inside theOuterStruct
structure (embedded nested struct);
struct OuterStruct
{
<fields>;
struct NestedStruct
{
<fields>
}nested;
};
- Declare the
NestedStruct
structure separately from theOuterStruct
structure, create an instance of theNestedStruct
structure inside theOuterStruct
structure (separate nested struct).
struct NestedStruct
{
<fields>;
};
struct OuterStruct
{
<fields>
struct NestedStruct nested;
};
The difference is that when a nested structure is declared separately, that structure can be used in many other structures.
If an instance of a nested structure was created outside of an external structure, then the structure will not be nested.
struct OuterStruct {
int outer_field;
struct NestedStruct {
int nested_field;
};
};
struct OuterStruct outer_struct;
struct NestedStruct inner_struct; // not nested struct
You can access the fields of a nested structure directly through the "." operator or through a pointer and the -> operator.
An example of using nested structures:
main.c
123456789101112131415161718192021222324252627#include <stdio.h> // structure for address struct Address { char street[50]; char city[50]; char index[10]; }; // structure for person struct Person { char name[50]; int age; struct Address address; // nested structure }; int main() { // initialization of Person structure struct Person person = { "Sherlock Holmes", 27, {"Baker Street", "London", "221B"} }; // display information about the person and his address printf("Name: %s\n", person.name); printf("Age: %d\n", person.age); printf("Address: %s, %s, %s\n", person.address.index, person.address.street, person.address.city); return 0; }
Swipe to start coding
- Define a nested structure
education
; - Create an instance of the
Student
structure; - Display the student's name and age;
- Display information about the student’s education.
Oplossing
Bedankt voor je feedback!
Awesome!
Completion rate improved to 4.17single