1. 程式人生 > 實用技巧 >6. 結構體

6. 結構體

struct 結構體名 {
成員資料型別  成員名:
}
 /*
 * 結構體
 * */
struct Student {
    string name;
    int age;
    int score;
};

int main(int argc, char const *argv[]) {
/*//結構體的初始化
//① struct Stduent s1
    struct Stduent structStduent;
    structStduent.age=20;
    structStduent.name="電風扇";
    std::cout << structStduent.name << std::endl;
//② struct Stduent s1={};
     Stduent s1={
            "離散",
            28,
            99
    };
    std::cout << s1.name << std::endl;
//③ 定義時建立變數
student.name="dfghsjkdfh";*/
//結構體陣列
    struct Student studentArr[3] =
            {
                    {"張三",      18, 100},
                    {"王五",      25, 66},
                    {"dfsdfsd", 58, 45}
            };
    //結構體指標
    Student  *pInt=&s1;
    std::cout <<  pInt->name << std::endl;

    studentArr[1].name = "lsdfhakjs";
    studentArr[2].age=25;
    return 0;
}