//函数返回结构的使用示例

本文通过一个C++程序示例介绍了如何定义并使用结构体来存储学生信息,并演示了如何通过函数返回结构体变量。
//函数返回结构的使用示例 
#include<iostream.h>
#include<conio.h> 
 struct student 
 {
    int num;
    char name[20];
    char sex;
    float score;       
        
 }; 
 
 student getstudent();
 void displaystudentinfo(const student &stud);
 
 int main()
 {
   student thestud={102,"li xiao ming",'M',92}; 
   
   cout<<"Intial student information:";
   displaystudentinfo(thestud); 
   
   thestud=getstudent();  //将右边结构类型的成员整体赋给左边 
   cout<<"\nAfter call getstudent:";
   displaystudentinfo(thestud);
    
      getch(); 
   return 0;  
 } 
 
student getstudent()     //返回值为student 型   
{
  student stud;
  
  cout<<"Please enter the number:";
  cin>>stud.num;
  
  cout<<"Please enter the name:";
  cin>>stud.name; 
  
  cout<<"Please enter the sex:";
  cin>>stud.sex;
  
  cout<<"Please enter the score:"; 
  cin>>stud.score;
          
       
  return stud;      //返回值仍然为值传递,类似return n(n为整型等基本数据类型); 
        
} 
 
void displaystudentinfo(const student &stud) //用传引用的方式 输出原始信息 ,为防止原数据被修改用const进行限定 
{
   cout<<endl;
   cout<<"num="<<stud.num<<"\t";
   cout<<"name="<<stud.name<<"\t";     
   cout<<"sex="<<stud.sex<<"\t";
   cout<<"score="<<stud.score<<"\t"<<endl;      
} 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值