C++结构体概述
在C++中,将一个结构体变量中的数据传递给另一个函数,有3种方法:?
经典案例:C++输出学生信息。
#include<iostream>//预处理
using?namespace?std;//命名空间?
??struct?Student{?//自定义结构体变量?
????int?num;//学号?
????char?sex;//性别?
????int?age;//年龄?
??};
int?main()//主函数?
{
??void?print_Function(Student stu);//函数声明?
??Student stu;
??stu.num=1001;
??stu.sex='F';
??stu.age=20;
??print_Function(stu);
??return?0;?//函数返回值为0;
}?
void?print_Function(Student stu)
{
??cout<<stu.num<<endl;//输出学号?
??cout<<stu.sex<<endl;//输出性别?
??cout<<stu.age<<endl;//输出年龄?
}
编译运行结果:
1001
F
20
--------------------------------
Process exited after?2.002?seconds?with?return?value?0
请按任意键继续. . .
更多案例可以go公众号:C语言入门到精通
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。