Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ Struct with Arrays and Other Structs | Section
C Structs
セクション 1.  13
single

single

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.

タスク

スワイプしてコーディングを開始

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.

解答

Switch to desktop実践的な練習のためにデスクトップに切り替える下記のオプションのいずれかを利用して、現在の場所から続行する
すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 1.  13
single

single

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

some-alt