Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Nested Structs | Advanced Structs Usage
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.

Aufgabe

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.

Lösung

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 4. Kapitel 1
single

single

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

close

bookNested Structs

Swipe um das Menü anzuzeigen

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.

Aufgabe

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.

Lösung

Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 4. Kapitel 1
single

single

some-alt