• 作用:将结构体作为参数向函数中传递
  • 传递方式有两种
    • 值传递
    • 地址传递 ```cpp

      include

      include

using namespace std;

// 定义学生结构体 struct Student { string name; int age; int score; };

void printStudent(struct Student std);

void printStudent1(struct Student *std);

int main() { // 结构体做函数的参数 // 将学生传入到一个参数中,打印学生身上的所有信息 // 创建结构体变量 struct Student std; std.name = “张三”; std.age = 22; std.score = 100; printStudent(std); printStudent1(&std); cout << “std.name “ << std.name << endl; cout << “std.age “ << std.age << endl; cout << “std.score _” << std.score << endl;

  1. return 0;

}

void printStudent(struct Student std) { std.name = “李思”; std.age = 20; std.score = 50; cout << “std.name” << std.name << endl; cout << “std.age” << std.age << endl; cout << “std.score” << std.score << endl; }

void printStudent1(struct Student *std) { std->name = “李思”; std->age = 20; std->score = 50; cout << “std1.name” << std->name << endl; cout << “std1.age” << std->age << endl; cout << “std1.score” << std->score << endl; } ```