◆结构体成员名与程序中变量名可相同,两者不代 表同一个对象。 struct student stu num i int num; char name 20; float score, stu; num int num Advanced Programming
Advanced Programming ❖结构体成员名与程序中变量名可相同,两者不代 表同一个对象。 struct student { int num; char name[20]; float score; }stu; int num; stu. num num
3、结构体变量的引用 ◆结构体变量不能整体引用,只能引用变量成员 结构体变量名成员名 struct student 符 f int num; stul,num=10: char name 201 结合性从左向右 char sex int age: stul age++, float score; char addr] stul score=85.5 3stul, stu2 stul score+=stu2 score Advanced Programming
Advanced Programming 3、 结构体变量的引用 ◆结构体变量不能整体引用,只能引用变量成员 结构体变量名.成员名 成员(分量)运算符 优先级: 1 结合性:从左向右 struct student { int num; char name[20]; char sex; int age; float score; char addr[30]; }stu1,stu2; stu1.num=10; stu1.age++; stu1.score=85.5; stu1.score+=stu2.score;
main( struct student f int no; float score: 3 stul, stu2 scanf(“%d,%f,&stu1);(×) scant(“%d,%f,& stul.no,&stul, score);(√) print(%d,%r”,stu1);(×) print%d,%r”,stu1.No,stul, score);(√) stu2=stul;(√) Advanced Programming
Advanced Programming main() { struct student { int No; float score; } stu1,stu2; } scanf(“%d,%f”,&stu1); () scanf(“%d,%f”,&stu1.No, &stu1.score); (√) printf(“%d,%f”,stu1); () printf(“%d,%f” , stu1.No, stu1.score); (√) stu2=stu1; (√)
◇结构体成员本身又是一个结构体类型,则需要找 到最低一级的成员。 struct student nt num char name[20];stul birthday month=12; struct date i int month; int day birthday num name int year; month day year ,birthday 3stul, stu2; ◇结构体变量的成员与普通变量用法相同。 Advanced Programming
Advanced Programming ❖结构体成员本身又是一个结构体类型,则需要找 到最低一级的成员。 struct student { int num; char name[20]; struct date { int month; int day; int year; }birthday; }stu1,stu2; num name birthday month day year stu1.birthday.month=12; ❖结构体变量的成员与普通变量用法相同
4、结构体变量的初始化 ◆形式 struct结构体名 类型标识符成员名1; 类型标识符成员名2; struct结构体名结构体变量={初始数据}; struct stu i int nums char name[ 201 int age: char addr 301 struct stu--a={112, Wang lin”,19,“200 Beijing Road”} Advanced Programming
Advanced Programming 4、 结构体变量的初始化 ◆形式一 struct 结构体名 { 类型标识符 成员名1; 类型标识符 成员名2; ……………. }; struct 结构体名 结构体变量={初始数据}; struct stu { int num; char name[20]; int age; char addr[30]; }; struct stu a={112,“Wang Lin”, 19, “200 Beijing Road”};