结构体是什么?
结构体是一种由一序列的成员组成的类型,字符为:struct ,而成员的存储 按顺序分配于内存中。
而在进行定义结构体的时候,不能向其他函数一样——可以先声明,后定义。结构体中,必须要声明同时定义,且要在 main 函数之前,不然在 main 函数中调用时就会报错。
结构体值传递
#include<iostream>
#include<iomanip>
/*
这个头文件只是用来调用 setw(),如果不写可以换成
“ ”(空字符)来将字符分开,以便区分观察。
*/
#include<string>
/*
string 类型的字符串会比 char[] 更简单,
我个人习惯于用 string 。此处char[]也可以。
*/
using namespace std;
struct student
{
string name;
int age; //定义年龄
int score; //定义分数
};
struct teacher //结构体 老师
{
string name;
int id;
int age;
struct student st;
};
void printarr(struct teacher th)
//打印出结构体中元素的值
{
th.id = 500;
cout << "老师姓名: " << th.name << setw(15)
<< "老师年龄: " << th.age << endl;
cout << "老师id:" << th.id<<endl;
cout << "学生姓名: " << th.st.name << '\n';
cout << "学生年龄: "