结构体定义和使用

语法:struct 结构体名 {结构体成员列表};
通过结构体创建变量的方式有三种:

  1. struct 结构体名 变量名
  2. struct 结构体名 变量名={成员1值,成员2值...}
  3. 定义结构体时顺便创建变量

第一种方式创建结构体

#include<iostream>
#include<string>
using namespace std;
struct student {
    string name;
    int age;
    int score;
};
int main() {
    struct student s1;
    s1.name = "张三";
    s1.age = 20;
    s1.score = 90;
    cout << "姓名:" << s1.name << endl;
    cout << "年龄:" << s1.age << endl;
    cout << "成绩:" << s1.score << endl;
    return 0;
}

第二种定义方式

#include<iostream>
#include<string>
using namespace std;
struct student {
    string name;
    int age;
    int score;
};
int main() {
    struct student s1 = { "zhangsan",18,90 };
    cout << "name:" << s1.name << endl;
    cout << "age:" << s1.age << endl;
    cout << "score:" << s1.score << endl;
    return 0;
}

第三种创建方式

#include<iostream>
#include<string>
using namespace std;
struct student {
    string name;
    int age;
    int score;
}s1;
int main() {
    s1.name = "李四";
    s1.age = 23;
    s1.score = 88;
    cout << "姓名" << s1.name << endl;
    cout << "年龄" << s1.age << endl;
    cout << "成绩" << s1.score << endl;
    return 0;
}

结构体在创建的时候struct可以省略,但是定义的时候不可以省略

结构体数组

语法:struct 结构体名 数组名[元素个数]={ {},{}.{}...{} }

#include<iostream>
#include<string>
using namespace std;
struct student {
    string name;
    int age;
    int score;
};
int main() {
    student stu[3] = { {"张三",18,90},{"李四",19,85},{"王五",17,95} };
    for(int i = 0; i < 3; i++) {
        cout << "姓名:" << stu[i].name << "\t年龄:" << stu[i].age << "\t成绩:" << stu[i].score << endl;
    }
    return 0;
}

结构体指针

结构体指针通过->来访问结构体里面的元素.

#include<iostream>
#include<string>
using namespace std;
struct student {
    string name;
    int age;
    int score;
};
int main() {
    student s1 = { "张三",20,90 };
    student* ps = &s1;
    cout << "姓名:" << ps->name << endl;
    cout << "年龄:" << ps->age << endl;
    cout << "成绩:" << ps->score << endl;
    return 0;
}

结构体嵌套结构体

#include<iostream>
#include<string>
using namespace std;
struct student {
    string name;
    int age;
    int score;
};
struct teacher {
    int id;
    string name;
    int age;
    struct student stu;
};
int main() {
    teacher t1 = { 1001, "Mr. Smith", 40, {"Alice", 20, 90} };
    cout << t1.id << endl;
    cout << t1.name << endl;
    cout << t1.age << endl;
    cout << t1.stu.name << endl;
    cout << t1.stu.age << endl;
    cout << t1.stu.score << endl;
    return 0;
}

结构体作为函数参数

  • 值传递
  • 地址传递
#include<iostream>
#include<string>
using namespace std;
struct student {
    string name;
    int age;
    int score;
};
//值传递
void print_student(student stu) {
    cout << "name: " << stu.name << endl;
    cout << "age: " << stu.age << endl;
    cout << "score: " << stu.score << endl;
}
//地址传递
void print_student(student* ps) {
    cout << "name: " << ps->name << endl;
    cout << "age: " << ps->age << endl;
    cout << "score: " << ps->score << endl;
}
int main() {
    student stu1 = { "张三", 20, 90 };
    print_student(stu1);
    student* ps = &stu1;
    print_student(ps);
    return 0;
}

结构体中const的使用场景

使用const防止误操作

#include<iostream>
#include<string>
using namespace std;
struct student {
    string name;
    int age;
    int score;
};
// 将函数中的形参改为指针,可以减少内存空间,而且不会复制新的副本
void print_student(const student* ps) {
    //ps->age = 30; 在加入const之后,不能修改指针所指向的内容
    cout << "name: " << ps->name << endl;
    cout << "age: " << ps->age << endl;
    cout << "score: " << ps->score << endl;
}
int main() {
    student stu1 = { "张三", 20, 90 };
    student* ps = &stu1;
    print_student(ps);
    return  0;
}

在函数中使用地址传递可以节省内存空间,再加上const就可以防止我们误操作修改结构体.