C++ structure type as function parameter | output student information

C++ structure overview

In C++, there are three ways to pass the data in a structure variable to another function:

Using structure variable names as parameters, this method is generally less used.
Use the pointer to the structure variable as the actual parameter, and pass the address of the structure variable to the formal parameter.
Use the reference variable of the structure variable as the function parameter.

Classic case: C++ outputs student information.

#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;//输出年龄 
}

Compile and run results:

1001
F
20

--------------------------------
Process exited after 2.002 seconds with return value 0
请按任意键继续. . .

C++ output student information
More cases can go to the public account: C language entry to proficient

Guess you like

Origin blog.csdn.net/weixin_48669767/article/details/112203547