1. #include <iostream> // 预处理
    2. #include <string> // 引入这个才能使用string类型
    3. using namespace std; // 引入命名空间
    4. struct student {
    5. string name;
    6. int score;
    7. };
    8. // 值传递,不能改变外部传入的值
    9. void printStudent1(student stu)
    10. {
    11. stu.name = "ccc";
    12. cout << stu.name << endl;
    13. }
    14. /*
    15. * 引用传递,可以改变外部传入的值,如果不想改变外部的值
    16. *,可以约定使用const的方式
    17. */
    18. void printStudent(const student *stu)
    19. {
    20. // stu->name = "666"; // 这里会提示报错
    21. cout << stu->name << endl;
    22. }
    23. int main() {
    24. student stu = {
    25. "abc",
    26. 100
    27. };
    28. printStudent1(stu); // ccc
    29. printStudent(&stu); // abc
    30. return 0;
    31. }