结构体类型的函数参数有哪些?
[1]结构体类型方法一用结构体变量作函数参数#includeiostream#includestringusingnamespacestd;{intnum;floatscore[3];};intmain(){voidprint(Student);//函数声明,形参类型为结构体StudentStudentstu;//定义结构体变量stu。 num12345;//以下5行对结构体变量各成员赋值stu。score[0]67。5;stu。score[1]89;stu。score[2]78。5;print(stu);//调用print函数,输出stu各成员的值return0;}voidprint(...全部
[1]结构体类型方法一用结构体变量作函数参数#includeiostream#includestringusingnamespacestd;{intnum;floatscore[3];};intmain(){voidprint(Student);//函数声明,形参类型为结构体StudentStudentstu;//定义结构体变量stu。
num12345;//以下5行对结构体变量各成员赋值stu。score[0]67。5;stu。score[1]89;stu。score[2]78。5;print(stu);//调用print函数,输出stu各成员的值return0;}voidprint(Studentst){coutst。
num″″″″st。score[0]″″st。score[1]″″st。score[2]endl;}运行结果为1234567。58978。5[1]结构体类型方法二用指向结构体变量的指针作实参#includeiostream#includestringusingnamespacestd;structStudent{intnum;stringname;//用string类型定义字符串变量floatscore[3];}stu{12345,″LiFung″,67。
5,89,78。
5};//定义结构体student变量stu并赋初值intmain(){voidprint(Student*);//函数声明,形参为指向Student类型数据的指针变量Student*ptstu;//定义基类型为Student的指针变量pt,并指向stuprint(pt);//实参为指向Student类数据的指针变量return0;}//定义函数,形参p是基类型为Student的指针变量voidprint(Student*p){coutpnum″″pname″″pscore[0]″″pscore[1]″″pscore[2]endl;}[1]调用print函数时,实参指针变量pt将stu的起始地址传送给形参p(p也是基类型为student的指针变量)。收起