c++结构体总结(结构体定义,结构体数组,结构体指针,结构体嵌套结构体,结构体做函数参数,结构体中 const使用场景)

看完b站黑马程序员之后的借鉴和笔记

1.什么是结构体,有什么作用?

在C/C++中,结构体是用户定义的数据类型它可以把几种不同类型的数据项集合成结构体这样一个单一类型

2. 结构体定义和使用

#include<iostream>
#include<string>
using namespace std;
struct student
{
//成员列表
    string name;  //姓名
    int age;      //年龄
    int score;    //分数
}
struct Student2
{
	string name;
	int age;
	int score;
}s3; //注意:第三种创建方式时,这里要标记以下
int main() {
	//结构体变量创建方式1
    struct student s1; //struct 关键字可以省略
    s1.name = "张三";
    s1.age = 18;
    s1.score = 100;
	cout << "name=" << s1.name << "  age=" << s1.age << "  score=" << s1.score << endl;

	//结构体变量创建方式2
	Student s2 = {"王五",18,97};
	cout << "name=" << s2.name << "  age=" << s2.age << "  score=" << s2.score << endl;

	//结构体变量创建方式3
	s3.name = "王五";
	s3.age = 17;
	s3.score = 78;
	cout << "name=" << s3.name << "  age=" << s3.age << "  score=" << s3.score << endl;
	system("pause");
	return 0;
}

总结1:定义结构体时的关键字是struct,不可省略
总结2:创建结构体变量时,关键字struct可以省略
总结3:结构体变量利用操作符 ''.''  访问成员

3.结构体数组

作用:将以上定义的张三,王五等结构体一起打包放在一个数组里面。

//结构体定义
struct student
{
    //成员列表
    string name;  //姓名
    int age;      //年龄
    int score;    //分数
}
int main() {
    
    //结构体数组
    struct student arr[3]=
    {
        {"张三",18,80 },
        {"李四",19,60 },
        {"王五",20,70 }
    };
    for (int i = 0; i < 3; i++)
    {
 //输出每个成员,数组是从0开始算的
        cout << "姓名:" << arr[i].name << " 年龄:" << arr[i].age << " 分数:" << 
arr[i].score << endl;
    }
    system("pause");
    return 0;
}

 4 结构体指针

通过指针访问结构体中的成员

//结构体定义
struct student
{
    //成员列表
    string name;  //姓名
    int age;      //年龄
    int score;    //分数
};
int main() {
    
    struct student stu = { "张三",18,100, };
    
    struct student * p = &stu;
    
    p->score = 80; //指针通过 -> 操作符可以访问成员
    cout << "姓名:" << p->name << " 年龄:" << p->age << " 分数:" << p->score << 
endl;
    
    system("pause");
    return 0;
}

5.结构体嵌套结构体

简单来说就是结构体里面还有结构体,套娃行为

//学生结构体定义
struct student
{
    //成员列表
    string name;  //姓名
    int age;      //年龄
    int score;    //分数
};
//教师结构体定义
struct teacher
{
    //成员列表
    int id; //职工编号
    string name;  //教师姓名
    int age;   //教师年龄
    struct student stu; //子结构体 学生
};
int main() {
    struct teacher t1;
    t1.id = 10000;
    t1.name = "老王";
    t1.age = 40;
    t1.stu.name = "张三";
    t1.stu.age = 18;
    t1.stu.score = 100;
    cout << "教师 职工编号: " << t1.id << " 姓名: " << t1.name << " 年龄: " << 
t1.age << endl;
    
    cout << "辅导学员 姓名: " << t1.stu.name << " 年龄:" << t1.stu.age << " 考试分
数: " << t1.stu.score << endl;
    system("pause");
    return 0;
}

6 结构体做函数参数

作用:将结构体作为参数向函数中传递
传递方式有两种:
值传递,不可以改变形参
地址传递,可改变形参

//学生结构体定义
struct student
{
    //成员列表
    string name;  //姓名
    int age;      //年龄
    int score;    //分数
};
//值传递
void printStudent(student stu )
{
    stu.age = 28;
    cout << "子函数中 姓名:" << stu.name << " 年龄: " << stu.age  << " 分数:" << 
stu.score << endl;
}
//地址传递
void printStudent2(student *stu)
{
    stu->age = 28;
    cout << "子函数中 姓名:" << stu->name << " 年龄: " << stu->age  << " 分数:" << 
stu->score << endl;
}
int main() {
    student stu = { "张三",18,100};
    //值传递
    printStudent(stu);
    cout << "主函数中 姓名:" << stu.name << " 年龄: " << stu.age << " 分数:" << 
stu.score << endl;
    cout << endl;
    //地址传递
    printStudent2(&stu);
    cout << "主函数中 姓名:" << stu.name << " 年龄: " << stu.age  << " 分数:" << 
stu.score << endl;
    system("pause");
    return 0;
}

总结:如果不想修改主函数中的数据,用值传递,反之用地址传递

7 结构体中 const使用场景 
作用:用const来防止误操作,因为const修饰的常量,不可修改。

//学生结构体定义
struct student
{
    //成员列表
    string name;  //姓名
    int age;      //年龄
    int score;    //分数
};
//const使用场景
void printStudent(const student *stu) //加const防止函数体中的误操作
{
    //stu->age = 100; //操作失败,因为加了const修饰
    cout << "姓名:" << stu->name << " 年龄:" << stu->age << " 分数:" << stu-
>score << endl;
}
int main() {
    student stu = { "张三",18,100 };
    printStudent(&stu);
    system("pause");
    return 0;
}

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值