Nested Structs
In C, structures can contain other structures as members, in addition to basic types like int or char, arrays, and pointers. Nested structures are useful when you want to group related data together in a logical way.
Embedded Nested Structure
An embedded nested structure is declared directly inside the outer structure. This means the nested structure exists only within the outer structure and cannot be used elsewhere.
main.c
123456789101112131415161718192021#include <stdio.h> struct Person { char name[50]; int age; struct Address { char street[50]; char city[50]; char index[10]; } address; // embedded nested structure }; int main() { struct Person person = { "Sherlock Holmes", 27, {"Baker Street", "London", "221B"} }; 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; }
In this example, the Address structure is defined inside Person, so it exists only in the context of the Person structure. You can access the fields of the nested structure using person.address, which allows you to retrieve street, city, and index. This approach is convenient when the nested data is specific to a single outer structure and does not need to be reused elsewhere.
Separate Nested Structure
A separate nested structure is declared independently and then included in the outer structure. This allows the nested structure to be reused in multiple outer structures or instantiated separately.
main.c
123456789101112131415161718192021222324#include <stdio.h> struct Address { char street[50]; char city[50]; char index[10]; }; struct Person { char name[50]; int age; struct Address address; // nested structure }; int main() { struct Address addr = {"Baker Street", "London", "221B"}; struct Person person = {"Sherlock Holmes", 27, addr}; 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; }
Here, Address is declared independently from Person, so it can be used on its own or inside multiple structures. When initializing a Person, we assign an Address instance to the address field. Accessing the nested fields works the same way using person.address. This approach is more flexible and encourages code reuse and modular design.
Swipe to start coding
Each product is represented by a structure Product that contains a nested structure Manufacturer. The function should iterate over an array of products, check if the product belongs to the specified manufacturer, and sum up the total value (price * quantity) of all matching products.
Implement a function calculateManufacturerTotal with a return type of float.
- Inside the function, create a
floatvariabletotalinitialized to0.0f. - Use a
forloop to iterate over the array of products from0ton. - For each product, compare the product's
manufacturer.companyNamewith thecompanyNameparameter usingstrcmp. - If it matches, add
price * quantitytototal. - Return the final value of
total.
Solution
Merci pour vos commentaires !
single
Demandez à l'IA
Demandez à l'IA
Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion
Can you show me an example of how to declare an embedded nested structure in C?
What are the advantages and disadvantages of using embedded versus separate nested structures?
How do I access the fields of a nested structure in C?
Génial!
Completion taux amélioré à 4.35
Nested Structs
Glissez pour afficher le menu
In C, structures can contain other structures as members, in addition to basic types like int or char, arrays, and pointers. Nested structures are useful when you want to group related data together in a logical way.
Embedded Nested Structure
An embedded nested structure is declared directly inside the outer structure. This means the nested structure exists only within the outer structure and cannot be used elsewhere.
main.c
123456789101112131415161718192021#include <stdio.h> struct Person { char name[50]; int age; struct Address { char street[50]; char city[50]; char index[10]; } address; // embedded nested structure }; int main() { struct Person person = { "Sherlock Holmes", 27, {"Baker Street", "London", "221B"} }; 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; }
In this example, the Address structure is defined inside Person, so it exists only in the context of the Person structure. You can access the fields of the nested structure using person.address, which allows you to retrieve street, city, and index. This approach is convenient when the nested data is specific to a single outer structure and does not need to be reused elsewhere.
Separate Nested Structure
A separate nested structure is declared independently and then included in the outer structure. This allows the nested structure to be reused in multiple outer structures or instantiated separately.
main.c
123456789101112131415161718192021222324#include <stdio.h> struct Address { char street[50]; char city[50]; char index[10]; }; struct Person { char name[50]; int age; struct Address address; // nested structure }; int main() { struct Address addr = {"Baker Street", "London", "221B"}; struct Person person = {"Sherlock Holmes", 27, addr}; 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; }
Here, Address is declared independently from Person, so it can be used on its own or inside multiple structures. When initializing a Person, we assign an Address instance to the address field. Accessing the nested fields works the same way using person.address. This approach is more flexible and encourages code reuse and modular design.
Swipe to start coding
Each product is represented by a structure Product that contains a nested structure Manufacturer. The function should iterate over an array of products, check if the product belongs to the specified manufacturer, and sum up the total value (price * quantity) of all matching products.
Implement a function calculateManufacturerTotal with a return type of float.
- Inside the function, create a
floatvariabletotalinitialized to0.0f. - Use a
forloop to iterate over the array of products from0ton. - For each product, compare the product's
manufacturer.companyNamewith thecompanyNameparameter usingstrcmp. - If it matches, add
price * quantitytototal. - Return the final value of
total.
Solution
Merci pour vos commentaires !
single