C/C++结构体
上节我们介绍了宏定义。C/C++之宏定义
本节我们介绍结构体。
定义
-
一个学生,有学号、姓名,年龄,班级、电话等等属性,这些复杂的属性可以归为一类,而单纯的数据类型很不方便。这个时候就可以使用结构体整合在一起
-
定义:
struct 结构名{
成员类型 成员名;
成员类型 成员名;
};
-
例如:
#include <iostream> #include <string.h> using namespace std; struct Student { int num; //学号 char name[16]; // 姓名 int age; //年龄 char tel[12]; //电话 }; int main() { //定义一个Student类型的变量 struct Student st1; return 0; }
-
注意:
- 要以struct开头。
- 每个成员后面有分号,最后的大括号也有分号。
- 结构体相当于程序员自己定义的数据类型。
初始化
-
定义了肯定得初始化。
-
初始化方式有三种方法,举例:
#include <iostream> #include <string.h> using namespace std; struct Student { int num; //学号 char name[16]; // 姓名 int age; //年龄 char tel[12]; //电话 }; int main() { //定义一个Student类型的变量并初始化 //第一种方式 struct Student st1 = {1, "小王", 12, "*******"}; //方式二初始化部分变量但在vc或者vs下无法编译,gcc支持 //struct Student st2 = {.name = "小明", .age = 11}; //方式三 struct Student st3; st3.num = 3, st3.age = 13; strcpy_s(st3.name, sizeof(st3.name), "小红"); return 0; }
-
另外,结构体里面也可以包含结构体:
#include <iostream> #include <string.h> using namespace std; struct Student { int num; //学号 char name[16]; // 姓名 int age; //年龄 char tel[12]; //电话 }; struct Class{ struct Student st1; struct Student st2; struct Student st3; }; int main() { //初始化 struct Class room1 = {{1, "小王", 12, "******"}, {2, "小明", 11, "******"}, {3, "小红", 13, "******"}}; //也可以 room1.st1.age = 15; return 0; }
调用
-
调用方式在上面例子的初始化方式三已经提到过了,这里再做个例子:
#include <iostream> #include <string.h> using namespace std; struct Student { int num; //学号 char name[16]; // 姓名 int age; //年龄 char tel[12]; //电话 }; int main() { //定义一个Student类型的变量并初始化 //第一种方式 struct Student st1 = {1, "小王", 12, "*******"}; cout << "学号:" << st1.num << " 姓名:" << st1.name << " 年龄:" << st1.age << " 电话:" << st1.tel << endl; return 0; }
输出结果:
学号:1 姓名:小王 年龄:12 电话:*******
-
提示: 结构体之间可以赋值。(仅限两个相同的结构体)
#include <iostream> #include <string.h> using namespace std; struct Student { int num; //学号 char name[16]; // 姓名 int age; //年龄 char tel[12]; //电话 }; int main() { //定义一个Student类型的变量并初始化 struct Student st1 = {1, "小王", 12, "*******"}; struct Student st2; st2 = st1; cout << "学号:" << st2.num << " 姓名:" << st2.name << " 年龄:" << st2.age << " 电话:" << st2.tel << endl; return 0; }
输出结果:
学号:1 姓名:小王 年龄:12 电话:*******
结构体数组
-
结构体也可以定义数组:
#include <iostream> #include <string.h> using namespace std; struct Student { int num; //学号 char name[16]; // 姓名 int age; //年龄 char tel[12]; //电话 }; int main() { //定义一个Student类型的变量并初始化 struct Student st[2]; st[0].num = 1, st[0].age = 12; strcpy_s(st[0].name, sizeof(st[0].name), "小王"); strcpy_s(st[0].tel, sizeof(st[0].tel), "******"); st[1] = st[0]; cout << "学号:" << st[1].num << " 姓名:" << st[1].name << " 年龄:" << st[1].age << " 电话:" << st[1].tel << endl; return 0; }
结果:
学号:1 姓名:小王 年龄:12 电话:*******
结构体指针
-
结构体指针和普通指针没啥太大的不同,唯一的不同就是他有多个成员
#include <iostream> #include <string.h> using namespace std; struct Student { char name[16]; // 姓名 char Gender[8]; //性别 int age; //年龄 }; int main() { //定义一个Student类型的变量并初始化 struct Student st = { "小王", "男", 12 }; //定义一个结构体指针 struct Student* P_st = &st; //通过 指针访问 cout << "姓名:" << P_st->name << " 性别:" << P_st->Gender << " 年龄:" << (*P_st).age << endl; return 0; }
输出结果:
姓名:小王 性别:男 年龄:12
注意:此处访问用了两种方式直接解引
((*P_st).age)等效于(P_st->age)
与指针访问(P_st->name)等效于((*P_st).name)
结构体传值
-
结构体传值(做函数参数)也是普通的值传递,不是地址传递。和普通的变量一样。
#include <iostream> #include <string.h> using namespace std; struct Student { int age; //年龄 int score; //成绩 }; void addScore(struct Student st, int x) { st.score += x; } int main() { //定义一个Student类型的变量并初始化 struct Student st = { 12, 59 }; addScore(st, 1); cout << "成绩:" << st.score << endl; return 0; }
输出:
成绩:59
由于普通的值传递,故此处无法修改.score。
下面进行使用指针,引用来进行传值:#include <iostream> #include <string.h> using namespace std; struct Student { int age; //年龄 int score; //成绩 }; //使用指针 void addScore(struct Student* st, int x) { st->score += x; } //使用引用 void addScore(struct Student& st, int x) { st.score += x; } struct Student* addScore1(struct Student st, int x) { st.score += x; return &st; } struct Student& addScore2(struct Student st, int x) { st.score += x; return st; } int main() { //定义一个Student类型的变量并初始化 struct Student st = { 12, 59 }; //使用指针 addScore(&st, 1); cout << "指针修改成绩:" << st.score << endl; //使用引用 addScore(st, 1); cout << "引用修改成绩:" << st.score << endl; //使用返回值是指针的函数 struct Student* P_s = addScore1(st, 1); cout << "返回值指针修改成绩:" << P_s->score << endl; //使用返回值是引用的函数 st = addScore2(st, 1); cout << "返回值引用修改成绩:" << st.score << endl; return 0; }
输出结果:
指针修改成绩:60 引用修改成绩:61 返回值指针修改成绩:62 返回值引用修改成绩:62
本节我们详细介绍了结构体的概念,下节我们介绍枚举变量。