113结构体变量的引用 1.引用结构体变量中的成员 格式:结构体变量名.成员名 struct student struct student st1 i char name[10; stl.name=“Mary”; int age i stl age= 21 float s1, s2 st1. s1=78 }; st1.s2=86 注意:一般是对结构体变量的各个成员分别进行赋值 stl={“Mary”,21,78,86};这样的赋值是不允许的
11.3 结构体变量的引用 1. 引用结构体变量中的成员 格式: 结构体变量名. 成员名 struct student { char name[10] ; int age ; float s1 , s2 ; } ; 注意: 一般是对结构体变量的各个成员分别进行赋值 st1 = { “Mary”, 21 , 78 , 86 } ; 这样的赋值是不允许的 struct student st1 ; st1. name = “Mary” ; st1. age = 21 ; st1. s1 = 78 ; st1. s2 = 86 ;
struct date struct stud st2 t int year int age, year int month i st2.name=“John”; int day i t2. age= 20 struct stud t2. birthday year=1980 i char name[101 st2 birthday. month=11; int age st2 birthday day= 23 struct date birthday st2.s1=89 float s1. s2 t2.s2=95 可以定义与结构体变量 age=24; 成员名相同名字的变量year=2000 它们之间不会发生混乱
struct date { int year ; int month ; int day ; } ; struct stud { char name[10] ; int age ; struct date birthday; float s1 , s2 ; } ; struct stud st2 ; int age , year ; st2. name = “John” ; st2. age = 20 ; st2. birthday. year = 1980 ; st2. birthday. month = 11 ; st2. birthday. day = 23 ; st2. s1 = 89 ; st2. s2 = 95 ; age = 24 ; year = 2000 ; 可以定义与结构体变量 成员名相同名字的变量 它们之间不会发生混乱
2.相同类型的结构体变量可以进行整体赋值 struct date struct stud stl st2 st3 i int year st1.name=“John” int month; stl age= 20 int day i stl birthday year =1980 stl birthday month=11 struct stud i char name[10 stl. birthday day=23 st1. sI=89 int age struct date birthday; st1.s2=95 float s1. s2 st2=stI 注意要正确赋值的条件 st3.name=“Mary”; 是变量st1已经有了数据 st3. age=20; st3. birthday=stl birthday st3.s1=76 st3.s2=85
2. 相同类型的结构体变量可以进行整体赋值 struct date { int year ; int month ; int day ; } ; struct stud { char name[10] ; int age ; struct date birthday; float s1 , s2 ; } ; struct stud st1, st2, st3; st1. name = “John” ; st1. age = 20 ; st1. birthday.year = 1980 ; st1. birthday.month = 11 ; st1. birthday.day = 23 ; st1. s1 = 89 ; st1. s2 = 95 ; st2=st1; st3. name=“Mary”; st3. age=20; st3. birthday=st1. birthday; st3. s1 = 76; st3. s2 = 85; 注意要正确赋值的条件 是变量st1已经有了数据
3.结构体变量的输入输出 C语言不允许结构体变量整体进行输入和输出, 只能对结构体变量的成员进行输入和输出 gets( stl name )i scanf(“%d%d%d”,&st1. birthday.year &stl birthday. month, &stl birthday day )i scanf ("%d %f%of', &stl. age, &stl. sl, &stl. S2); puts( sl name )i printf(“%4d”,stl.age) printf(“%d.%d.%d”,stl. birthday.year, stl birthday. month, stl birthday day printf(o5.2f%5.2f\n",stI sl, sIt. s2)
3. 结构体变量的输入输出 C语言不允许结构体变量整体进行输入和输出, 只能对结构体变量的成员进行输入和输出 gets( st1. name ) ; scanf( “%d%d%d”, &st1. birthday. year , &st1. birthday. month , &st1. birthday. day ) ; scanf ( “%d%f%f”, &st1. age , &st1. s1 , &st1. s2 ) ; puts( s1. name ) ; printf( “%4d”, st1. age ); printf( “%d .%d .%d”, st1. birthday. year , st1. birthday. month , st1. birthday. day ) ; printf(“%5.2f %5.2f\n”, st1. s1 , s1t. s2 ) ;
114结构体变量的初始化 struct student struct student i char name[10j; i char name[10li int age int age float scorel. score2 float scorel. score2 }st=“Mary”,21,78,86}; struct student stI 这是初始化,正确st1={“Mary”,2l,78,86}; struct stud i char name[10li 这是赋值,错误 e1, score2."i' struct date birthda C不允许这么做 float score struct stud st2={“John”, 1980,11,23,89,95};
11.4 结构体变量的初始化 struct student { char name[10] ; int age ; float score1 , score2 ; } st1={“ Mary”, 21, 78, 86} ; struct stud { char name[10] ; struct date birthday ; float score1 , score2 ; } ; struct stud st2={ “John” , 1980 , 11 , 23 , 89 , 95 } ; struct student { char name[10] ; int age ; float score1 , score2 ; } ; struct student st1; 这是初始化, 正确 st1={“ Mary”, 21, 78, 86} ; 这是赋值, 错误 C不允许这么做