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

Task

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

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 1
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

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

Swipe to show 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.

Task

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 desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 1
single

single

some-alt