使用结构体作为返回值,可以使得函数的调用更加灵活和方便。 /**//************************************************************************/ /**//* 使用结构体作为返回值 */ /**//************************************************************************/ #include "iostream.h" struct student { int id; char name[20]; int age; char department[20]; float gpa; }; student init(); //初始化,返回一个student的结构 void display(student arg); //显示结构体 int main() { display(init()); //init()返回的是一个student结构体 return 0; } void display(student arg) { cout<<"学号:"<<arg.id<<" 姓名:"<<arg.name<<" 年龄"<<arg.age<<" 专业:"<<arg.department<<" 成绩:"<<arg.gpa<<endl; } student init() { student s1 = {3221,"Tom",18,"Computer",86.33}; //初始化一个student的结构体 return s1; //返回一个student的结构体s1 } 网摘于http://www.cppblog.com/longhr/archive/2008/10/24/64923.aspx