結構體-結構體數組


 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 
 5 // 結構體數組
 6 
 7 struct Student   // 1、定義結構體
 8 {
 9     string name; // 姓名
10     int age;     // 年齡
11     int score;   // 分數
12 };
13 
14 int main() {
15     struct Student stuArray[3] =  // 創建結構體數組
16     {
17         {"張三",18,100},
18         {"李四",28,99},
19         {"王五",38,66}
20     };
21 
22     stuArray[2].name = "趙六";    // 給結構體數組中的元素賦值
23     stuArray[2].age = 80;
24     stuArray[2].score = 60;
25 
26     for (int i = 0; i < 3; i++)
27     {
28         cout << "姓名: " << stuArray[i].name
29             << " 年齡: " << stuArray[i].age
30             << " 分數: " << stuArray[i].score << endl;
31     }
32 }

程序輸出結果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM