(8.55)--13.3C语言程序设计结构体与指针.ppt
C语言程序设计三、结构体与指针1.指向结构体变量的指针指向结构体变量的指针也称为结构体指针,它保存了结构体变量的存储首地址。struct student int num;char name20;char sex;int age;float score;char addr30;;结构体指针的定义形式:struct 结构体类型名 *指针变量名;struct student stu,*p;p=&stu;三、结构体与指针1.指向结构体变量的指针struct student int num;char name20;char sex;int age;float score;char addr30;;struct student stu,*p;p=&stu;结构体变量成员的引用形式:结构体变量名.成员名;stu.age=20;(*p).age=20;p-age=20;其中-为指向成员运算符三、结构体与指针1.指向结构体变量的指针结构体变量成员的三种访问方法结构体变量.成员名(*结构体指针).成员名 结构体指针-成员名 三、结构体与指针2.指向结构体数组的指针struct personchar name10;int age;struct person *p,s,boy3=Zhang,18,Wang,20,Li,17;p=boy;三、结构体与指针2.指向结构体数组的指针struct personchar name10;int age;struct person *p;struct person boy3=Zhang,18,Wang,20,Li,17;p=boy;相当于p=&boy0Zhang18Wang20Li17pp+1p+2boy0boy1三、结构体与指针2.指向结构体数组的指针main()struct person char name10;int age;*p,boy3=Zhang,18,Wang,20,Li,17;for(p=boy;pname,(*p).age);for(p=boy,i=0;i3;i+)printf(“%s%dn”,pi.name,pi.age);int i;三、结构体与指针3.用结构体变量作为函数参数结构体变量作为函数参数,实现值传递指向结构体的指针(或数组)作为函数参数,实现地址传递#includestruct STUint num;float TotalScore;void f(struct STU p)struct STU s2=20044,550,20045,537;p.num=s1.num;p.TotalScore=s1.TotalScore;main()struct STU s2=20041,703,20042,580;f(s0);printf(%d%3.0fn,s0.num,s0.TotalScore);三、结构体与指针#includestruct STUint num;float TotalScore;void f(struct STU*p)struct STU s2=20044,550,20045,537;(*p).num=s1.num;(*p).TotalScore=s1.TotalScore;main()struct STU s2=20041,703,20042,580;f(&s0);printf(%d%3.0fn,s0.num,s0.TotalScore);三、结构体与指针三、结构体与指针struct STU char name10;int num;int Score;main()struct STU s5=YangSan,20041,703,LiSiGuo,20042,580,WangYin,20043,680,SunDan,20044,550,PengHua,20045,537,*p5,*t;int i,j;for(i=0;i5;i+)pi=&si;for(i=0;i4;i+)for(j=i+1;jScorepj-Score)t=pi;pi=pj;pj=t;for(i=0;iname,(*pi).num,pi-Score);感谢聆听