结构体
基本概念:结构体属于用户自定义的数据类型,允许用户储存不同的数据类型。
1、结构体的定义和使用:
#include <string>
//C++中使用字符串需要添加string头文件
#include "iostream"
using namespace std;
//1、创建学生数据类型,包括姓名年龄分数
//struct 类型名称 { 成员列表 }
struct Student
{
string name;
int age;
int score;
}wangwu;
//创建结构体的时候顺便添加成员(较少用)
int main()
{
//2、创建具体学生
struct Student zhangsan;
//创建成员时struct可以省略
//给zhangsan赋值,通过zhangsan.xx访问结构体变量中的属性
zhangsan.name = "张三" ;
zhangsan.age = 19;
zhangsan.score = 90;
struct Student lisi = {
"李四",18,92 };
//创建成员时直接赋值
return 0 ;
}
2、结构体数组
#include <string>
//C++中使用字符串需要添加string头文件
#include "iostream"
using namespace std;
struct Student
{
string name;
int age;
int score;
};
int main()

本文介绍了C++中的结构体基础知识,包括结构体的基本概念、定义和使用、结构体数组、结构体指针、结构体嵌套结构体以及结构体在函数参数中的应用,并详细讨论了const在结构体中的作用——防止误操作。
最低0.47元/天 解锁文章
3863

被折叠的 条评论
为什么被折叠?



