1 struct student 2 { 3 int id; 4 char name[10]; 5 6 };
注:student是自定義的數據類型,並不是變量像int,char 是基本數據類型,
1 struct student 2 { 3 int id; 4 char name[10]; 5 } mike,lily; //直接定義兩個結構體變量
1 student mike ={123,{'m','i','k','e'}};//定義變量並初始化 2 mike.id = 20130000+ mike.id //使用 3 for(int i = 0; mike.name[i] != '\0'; i++) 4 mike.name[i]= toupper(mike.name[i]); 5 cout<<"ID: "<<mike.id<<"name: "<<mike.name<<endl;//
一個結構體變量占有一片連續的內存空間
1 student mike ={123,{'m','i','k','e'}}; 2 student lily; 3 lily = mike;//可以直接賦值,對應的變量也賦值
與變量做函數參數相同,形參和實參直接進行值傳遞時,只copy一個副本,與數組名作參數不同
也是copy一個副本進行賦值
student mike={123,{'m','i','k','e'}}; student *ps = &mike; cout<<"ID: "<<(*ps).id<<"name: " <<(*ps).name<<endl; cout<<"ID:"<<ps->id<<"name: "<<ps->name<<endl;
1 struct student 2 { 3 int id; 4 char name[10]; 5 } stu[10];