天天看點

C++結構體數組

結構體數組

#include <iostream>

#include <string>

using namespace std;

//結構體數組

//1.定義結構體

struct Student

{

       //姓名

       string name;

       //年齡

       int age;

       //分數

       int score;

};

int main()

{

       //2.建立結構體數組

       struct Student stuArray[3]=

       {

              {"張三",18,100},

              {"李四",28,99},

              {"王五",38,66}

       };

       //3.給結構體數組中的元素指派

       stuArray[2].name = "趙六";

       stuArray[2].age = 18;

       stuArray[2].score = 80;

       //4.周遊結構體數組

       for(int i = 0; i < 3; i++)

       {

              cout << " 姓名:" << stuArray[i].name

                     << " 年齡:" << stuArray[i].age

                     << " 分數:" << stuArray[i].score << endl;

       }

       return 0;

}      

繼續閱讀