Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Nested Structs | Struct Usage
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
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

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 1

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

bookNested Structs

Scorri per mostrare il 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

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 1
some-alt