#include<iostream>
using namespace std;
struct student
{
char name[100];
int age;
};
//这里结构体是通过指针进行传递的,一般来说就是4个字节
void print_info(struct student *p)
{
cout << "姓名:" << p->name << " " << "年龄:" << p->age <<endl;
}
//这里结构体是通过赋值进行传递的,拷贝之后所占用的内存比较多,假如结构体里面成员较多,对于内存而言很可能是吃不消的
void print_info1(struct student s)
{
cout << "姓名:" << s.name << " " << "年龄:" << s.age << endl;
}
//这里结构体是通过引用进行传递的,也就是结构体的别名,这样来说,所占用的内存空间也是很小的
void print_info2(struct student &s)
{
cout << "姓名:" << s.name << " " << "年龄:" << s.age << endl;
}
int main(void)
{
student stu = { "zhx",25 };
print_info(&stu);
print_info1(stu);
print_info2(stu);
system("pause");
return 0;
}
这里定义了一个结构体,引用相对于指针来讲稍微简单一些,是变量的别名,应当注意使用引用。