Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Struct with Arrays and Other Structs | Advanced Structs Usage
C Structs

bookStruct with Arrays and Other Structs

In some structures, it becomes necessary to use an array of nested structures when one object contains multiple related sub-objects. For example, if a student can take several courses, it makes sense to represent the courses as an array of structures inside the Student structure.

Example syntax:

OuterStruct.NestedStruct[1].NestedField;

To access an element of the nested structure array, you first refer to the outer structure, then use an index to select the desired element of the array, and finally access a specific field of that element.

Practical Example

Imagine each student has a name, age, and a set of courses. Each course contains its title, instructor, and number of credits.

main.c

main.c

copy
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
#include <stdio.h> #include <string.h> struct Course { char name[50]; char instructor[50]; int credits; }; struct Student { char name[50]; int age; struct Course courses[3]; // array of nested structures }; int main() { struct Student student; // Initialize student strcpy(student.name, "Alice Johnson"); student.age = 20; // Initialize courses strcpy(student.courses[0].name, "Mathematics"); strcpy(student.courses[0].instructor, "Dr. Smith"); student.courses[0].credits = 4; strcpy(student.courses[1].name, "Computer Science"); strcpy(student.courses[1].instructor, "Prof. Brown"); student.courses[1].credits = 3; strcpy(student.courses[2].name, "History"); strcpy(student.courses[2].instructor, "Dr. Green"); student.courses[2].credits = 2; // Display information printf("Student: %s\n", student.name); printf("Age: %d\n", student.age); printf("Courses:\n"); for (int i = 0; i < 3; i++) { printf(" Course %d: %s, Instructor: %s, Credits: %d\n", i + 1, student.courses[i].name, student.courses[i].instructor, student.courses[i].credits); } // Example of accessing a specific nested field printf("\nInstructor of the second course: %s\n", student.courses[1].instructor); return 0; }

In this example, the Student structure has a courses array with three elements of type Course. Each course has its own title, instructor, and number of credits. To access a specific course, use the array index and dot operator: student.courses[1].instructor.

Using arrays of nested structures is useful for representing objects with repeated sub-objects, such as students with courses, stores with products, or books with authors.

Task

Swipe to start coding

The student is represented by a structure Student which contains a fixed-size array of nested structures Course. Each course has a name, instructor, and number of credits. The function should iterate through all courses and sum the credits.

Implement a function calculateTotalCredits with a return type of int.

  1. Inside the function, create an int variable total initialized to 0.
  2. Use a for loop to iterate from 0 to the number of courses (3).
  3. For each course, access credits and add it to total.
  4. Return the final value of total.

Solution

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 2
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 a code example of how to define these nested structures?

How do I access or modify a specific course for a student?

What are some other real-world scenarios where nested structure arrays are useful?

close

bookStruct with Arrays and Other Structs

Swipe to show menu

In some structures, it becomes necessary to use an array of nested structures when one object contains multiple related sub-objects. For example, if a student can take several courses, it makes sense to represent the courses as an array of structures inside the Student structure.

Example syntax:

OuterStruct.NestedStruct[1].NestedField;

To access an element of the nested structure array, you first refer to the outer structure, then use an index to select the desired element of the array, and finally access a specific field of that element.

Practical Example

Imagine each student has a name, age, and a set of courses. Each course contains its title, instructor, and number of credits.

main.c

main.c

copy
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
#include <stdio.h> #include <string.h> struct Course { char name[50]; char instructor[50]; int credits; }; struct Student { char name[50]; int age; struct Course courses[3]; // array of nested structures }; int main() { struct Student student; // Initialize student strcpy(student.name, "Alice Johnson"); student.age = 20; // Initialize courses strcpy(student.courses[0].name, "Mathematics"); strcpy(student.courses[0].instructor, "Dr. Smith"); student.courses[0].credits = 4; strcpy(student.courses[1].name, "Computer Science"); strcpy(student.courses[1].instructor, "Prof. Brown"); student.courses[1].credits = 3; strcpy(student.courses[2].name, "History"); strcpy(student.courses[2].instructor, "Dr. Green"); student.courses[2].credits = 2; // Display information printf("Student: %s\n", student.name); printf("Age: %d\n", student.age); printf("Courses:\n"); for (int i = 0; i < 3; i++) { printf(" Course %d: %s, Instructor: %s, Credits: %d\n", i + 1, student.courses[i].name, student.courses[i].instructor, student.courses[i].credits); } // Example of accessing a specific nested field printf("\nInstructor of the second course: %s\n", student.courses[1].instructor); return 0; }

In this example, the Student structure has a courses array with three elements of type Course. Each course has its own title, instructor, and number of credits. To access a specific course, use the array index and dot operator: student.courses[1].instructor.

Using arrays of nested structures is useful for representing objects with repeated sub-objects, such as students with courses, stores with products, or books with authors.

Task

Swipe to start coding

The student is represented by a structure Student which contains a fixed-size array of nested structures Course. Each course has a name, instructor, and number of credits. The function should iterate through all courses and sum the credits.

Implement a function calculateTotalCredits with a return type of int.

  1. Inside the function, create an int variable total initialized to 0.
  2. Use a for loop to iterate from 0 to the number of courses (3).
  3. For each course, access credits and add it to total.
  4. 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Β 2
single

single

some-alt