10.1.3访问结构成员例5:struct Date{int year;int month;int day;I todaytoday.year = 2021;today.month = 12;today.day = 9;
例5: struct Date { int year; int month; int day; } today ; today.year = 2021; today.month = 12; today.day = 9;
10.1.3访问结构成员structWeatherY例6://温度double temp;void main()//风力double wind;1:JIWeather wtoday;/ 作为左值使用wtoday.temp=30.5;wtoday.wind = 10.1 ;;/作为右值使用cout<<"Temp="<<wtoday.temp<<endl;cout<<"Wind="<<wtoday.wind<<endl;1
例6: void main( ) { Weather wtoday; wtoday.temp = 30.5; // 作为左值使用 wtoday.wind = 10.1 ; cout<<“Temp=”<<wtoday.temp<<endl; //作为右值使用 cout<<“Wind=”<<wtoday.wind<<endl; } struct Weather { double temp; //温度 double wind; //风力 } ;
10.1.3访问结构成员,结构体变量的成员的运算struct student1intnum;charname[20];floatscore,I studentl, student2 :student1={1234,"Frank",96.5];student2.score = student1.score;sum = student1.score + student2.score;
• 结构体变量的成员的运算 struct student { int num; char name[20]; float score; }student1, student2 ; student1 = { 1234, "Frank", 96.5}; student2.score = student1.score; sum = student1.score + student2.score;
10.1.4给结构赋值数组不能互相整体赋值因为数组名本质上是一个指针常量,不允许被赋值。数组是单纯空间意义上相同类型数据实体的集合。,使用数组下标1操作对单个元素进行访问,但不能对数组进行批量元素操作
• 数组不能互相整体赋值。 • 因为数组名本质上是一个指针常量,不允许被 赋值。 • 数组是单纯空间意义上相同类型数据实体的集 合。 • 使用数组下标[ ]操作对单个元素进行访问。 • 但不能对数组进行批量元素操作
10.1.4给结构赋值例7:void fn()(char a[10];char b[10] =“test";a= b;//error:数组不能整体赋值7
例7: void fn( ) { char a[10]; char b[10] = “test”; a = b; //error:数组不能整体赋值 }