Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Nested Structs | Struct Usage
C++ Structures and Enumerations

bookNested Structs

Nested structs allow you to represent more complex, hierarchical data by defining a struct within another struct. This is useful when modeling real-world objects that naturally contain other objects as part of their structure. For example, a Person might have an Address as one of its properties. By nesting an Address struct inside a Person struct, you can group related information together and keep your code organized and easy to understand.

main.cpp

main.cpp

copy
123456789101112131415161718192021222324252627282930313233
#include <iostream> #include <string> struct Address { std::string street; std::string city; int zipCode; }; struct Person { std::string name; int age; Address address; }; int main() { Address addr; addr.street = "123 Maple Ave"; addr.city = "Springfield"; addr.zipCode = 12345; Person person; person.name = "Alice"; person.age = 30; person.address = addr; std::cout << "Name: " << person.name << std::endl; std::cout << "Age: " << person.age << std::endl; std::cout << "Address: " << person.address.street << ", " << person.address.city << ", " << person.address.zipCode << std::endl; }

To access or modify a member of a nested struct, use the dot operator (.) repeatedly. For instance, if you have a Person variable called person, you can access the city in their address with person.address.city. This approach allows you to work with deeply structured data in a clear and straightforward way. If you need to update the zip code, you can assign a new value like person.address.zipCode = 54321;. This makes it easy to manage and manipulate nested information within your structs.

question mark

Which expression correctly accesses the city of the person's address?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 2. Chapitre 1

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

Awesome!

Completion rate improved to 8.33

bookNested Structs

Glissez pour afficher le menu

Nested structs allow you to represent more complex, hierarchical data by defining a struct within another struct. This is useful when modeling real-world objects that naturally contain other objects as part of their structure. For example, a Person might have an Address as one of its properties. By nesting an Address struct inside a Person struct, you can group related information together and keep your code organized and easy to understand.

main.cpp

main.cpp

copy
123456789101112131415161718192021222324252627282930313233
#include <iostream> #include <string> struct Address { std::string street; std::string city; int zipCode; }; struct Person { std::string name; int age; Address address; }; int main() { Address addr; addr.street = "123 Maple Ave"; addr.city = "Springfield"; addr.zipCode = 12345; Person person; person.name = "Alice"; person.age = 30; person.address = addr; std::cout << "Name: " << person.name << std::endl; std::cout << "Age: " << person.age << std::endl; std::cout << "Address: " << person.address.street << ", " << person.address.city << ", " << person.address.zipCode << std::endl; }

To access or modify a member of a nested struct, use the dot operator (.) repeatedly. For instance, if you have a Person variable called person, you can access the city in their address with person.address.city. This approach allows you to work with deeply structured data in a clear and straightforward way. If you need to update the zip code, you can assign a new value like person.address.zipCode = 54321;. This makes it easy to manage and manipulate nested information within your structs.

question mark

Which expression correctly accesses the city of the person's address?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 2. Chapitre 1
some-alt