C++结构体与函数—结构体作为函数参数和返回值

2020-01-06 12:21:08王冬梅

{
   display(initial());//输出返回的结构
   return 0;
}
void display(student arg)
{
   cout <<"学号:" <<arg.idNumber <<"姓名:" <<arg.name <<"年龄:" <<arg.age <<endl <<"院系:" <<arg.department <<"成绩:" <<arg.gpa <<endl;
}
student initial()
{
   student s1={428004, "Tomato",20, "ComputerScience",84.5};//初始化结构变量
   return s1;//返回结构
}

运行结果:
学号:428004姓名:Tomato年龄:20
院系:ComputerScience成绩:84.5