Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Nested Structs | Advanced Structs Usage
Quizzes & Challenges
Quizzes
Challenges
/
C Structs

bookNested 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

main.c

copy
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

main.c

copy
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.

Tâche

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.

  1. Inside the function, create a float variable total initialized to 0.0f.
  2. Use a for loop to iterate over the array of products from 0 to n.
  3. For each product, compare the product's manufacturer.companyName with the companyName parameter using strcmp.
  4. If it matches, add price * quantity to total.
  5. Return the final value of total.

Solution

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 4. Chapitre 1
single

single

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

Suggested prompts:

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?

close

bookNested 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

main.c

copy
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

main.c

copy
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.

Tâche

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.

  1. Inside the function, create a float variable total initialized to 0.0f.
  2. Use a for loop to iterate over the array of products from 0 to n.
  3. For each product, compare the product's manufacturer.companyName with the companyName parameter using strcmp.
  4. If it matches, add price * quantity to total.
  5. Return the final value of total.

Solution

Switch to desktopPassez à un bureau pour une pratique réelleContinuez d'où vous êtes en utilisant l'une des options ci-dessous
Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 4. Chapitre 1
single

single

some-alt