Struct 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
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.
Swipe to start coding
-
In the
Coursestructure, declare an arraycourseName, which will contain the name of the course; -
In the
Studentstructure, declare an arraystudentNameto store the student's name. -
In the
Studentstructure, initialize thecoursesarray, consisting of two nestedCoursestructures. -
Use a loop to display course information.
Solução
Obrigado pelo seu feedback!
single
Pergunte à IA
Pergunte à IA
Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo
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?
Awesome!
Completion rate improved to 4.35
Struct with Arrays and Other Structs
Deslize para mostrar o 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
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.
Swipe to start coding
-
In the
Coursestructure, declare an arraycourseName, which will contain the name of the course; -
In the
Studentstructure, declare an arraystudentNameto store the student's name. -
In the
Studentstructure, initialize thecoursesarray, consisting of two nestedCoursestructures. -
Use a loop to display course information.
Solução
Obrigado pelo seu feedback!
single