IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> C++ 结构体 -> 正文阅读

[C++知识库]C++ 结构体

概念

结构体属于用户自定义的数据类型,允许用户存储不同的数据类型?

语法

struct 结构体名 { 结构体成员列表 };?

通过结构体创建变量的方式有三种:struct 关键字可以去掉

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

#include <iostream>

using namespace std;
// 定义结构体
struct student{
    string name;
    int age;
    string hobby[3];
}stu3;  // 实例化结构体方式三  定义结构体时实例化

int main(){
    // 实例化结构体方式一:
    struct student stu1 = student{"haha", 20, {"python", "golang", "c++"}};
    cout << "姓名:" << stu1.name << " 年龄:" << stu1.age << " 爱好:" << stu1.hobby[0] << "," << stu1.hobby[1] << "," << stu1.hobby[2] << endl;
    // 实例化结构体方式二:
    struct student stu2;
    stu2.name = "hehe";
    stu2.age = 19;
    stu2.hobby[0] = "c++";
    cout << "姓名:" << stu2.name << " 年龄:" << stu2.age << " 爱好:" << stu2.hobby[0] << endl;

    stu3.name = "heihei";
    stu3.age = 18;
    stu3.hobby[0] = "python";
    stu3.hobby[1] = "c++";
    cout << "姓名:" << stu3.name << " 年龄:" << stu3.age << " 爱好:" << stu3.hobby[0] << "," << stu3.hobby[1] << endl;

    cout << sizeof(stu1) << endl;  // 136
    cout << sizeof(stu2) << endl;  // 136
    cout << sizeof(stu3) << endl;  // 136
    cout << sizeof(struct student) << endl;  // 136

}

总结1:定义结构体时的关键字是struct,不可省略

总结2:结构体定义完成后,所占与的字节数就已经确定

总结3:结构体通过 ' . '?访问成员,或为成员变量赋值

结构体数组

作用:将自定义的结构体放入到数组中方便维护

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

#include <iostream>

using namespace std;
// 定义结构体
struct student{
    string name;
    int age;
    string hobby[3];
}

int main(){
    // 结构体定义完成后,所占与的字节数就已经确定
    cout << sizeof(struct student) << endl;

    // 表示数组arr内的元素类型是 student 结构体
    student arr[3] = {
            {"haha", 20, {"python", "golang", "c++"}},
            {"hehe", 19, {"c++"}},
            {"heihei", 18, {"python", "c++"}},
    };
    for (int i = 0; i < sizeof(arr)/ sizeof(student); ++i) {
        cout << "姓名: " << arr[i].name << endl;
    }

}

结构体指针

  • 利用操作符? ->?可以通过结构体指针访问结构体属性
#include <iostream>

using namespace std;
// 定义结构体
struct student{
    string name;
    int age;
    string hobby[3];
};

int main(){

    struct student stu1 = student{"haha", 20, {"python", "golang", "c++"}};

    // 结构体指针
    struct student * p = &stu1;
    cout << p->hobby << endl;  // 0x7ffd0edc1458
    cout << *p->hobby << endl;  // python  解释: *p->hobby 等价于 *(p->hobby) 等价于 p->hobby[0] 等价于 stu1.hobby[0]
    cout << p->hobby[0] << endl; // python  等价于 stu1.hobby[0]

    cout << p->name << endl;  // haha  等价于 stu1.name
    cout << p->age << endl;  // 20  等价于 stu1.age
}

结构体嵌套

结构体中的成员可以是另一个结构体

#include <iostream>

using namespace std;

struct student{
    string name;
    int age;
    string hobby[3];
};

struct teacher{
    int id;
    string name;
    struct student stu;
};

int main(){
    // 实例化结构体方式一:
    struct student stu1 = student{"haha", 20, {"python", "golang", "c++"}};

    // 结构体嵌套
    struct teacher tea;
    tea.id = 1;
    tea.name = "wang";
    tea.stu = stu1;

    cout << "teacher id = " << tea.id
        << " teacher name= " << tea.name
        << " student name= " << tea.stu.name
        << " student age= " << tea.stu.age << endl;

}

结构体做函数参数

将结构体作为参数向函数中传递

传递方式有两种:

  • 值传递
  • 地址传递
#include <iostream>

using namespace std;
// 定义结构体
struct student{
    string name;
    int age;
    string hobby[3];
};


// 值传递
void printStudent(student stu){
    stu.age = 22;
    cout << "值传递中打印:" << endl;
    cout << "姓名:" << stu.name
         << " 年龄:" << stu.age
         << " 爱好:" << stu.hobby[0] << "," << stu.hobby[1] << "," << stu.hobby[2] << endl;
}

// 地址传递
void printStudent2(student * stu){
    stu->age = 25;
    cout << "地址递中打印:" << endl;
    cout << "姓名:" << stu->name
         << " 年龄:" << stu->age
         << " 爱好:" << stu->hobby[0] << "," << stu->hobby[1] << "," << stu->hobby[2] << endl;
}

int main(){
    // 实例化结构体方式一:
    struct student stu1 = student{"haha", 20, {"python", "golang", "c++"}};
    cout << "初始值:" << endl;
    cout << "姓名:" << stu1.name
        << " 年龄:" << stu1.age
        << " 爱好:" << stu1.hobby[0] << "," << stu1.hobby[1] << "," << stu1.hobby[2] << endl;


    printStudent(stu1);
    cout << "值传递后打印:" << endl;
    cout << "姓名:" << stu1.name
         << " 年龄:" << stu1.age
         << " 爱好:" << stu1.hobby[0] << "," << stu1.hobby[1] << "," << stu1.hobby[2] << endl;
    printStudent2(&stu1);
    cout << "地址递后打印:" << endl;
    cout << "姓名:" << stu1.name
         << " 年龄:" << stu1.age
         << " 爱好:" << stu1.hobby[0] << "," << stu1.hobby[1] << "," << stu1.hobby[2] << endl;

}

>>>>:
初始值:
姓名:haha 年龄:20 爱好:python,golang,c++
值传递中打印:
姓名:haha 年龄:22 爱好:python,golang,c++
值传递后打印:
姓名:haha 年龄:20 爱好:python,golang,c++
地址递中打印:
姓名:haha 年龄:25 爱好:python,golang,c++
地址递后打印:
姓名:haha 年龄:25 爱好:python,golang,c++

结构体中 const使用场景

作用:用const来防止误操作

#include <iostream>

using namespace std;
// 定义结构体
struct student{
    string name;
    int age;
    string hobby[3];
};

// 地址传递, 使用地址传递的意义是为了节省内存
void printStudent2(const student * stu){  // const 修饰了指针 -- 变量指针
    stu->age = 25;  // 报错 指针指向的值不可以修改
}

int main(){
    struct student stu1 = student{"haha", 20, {"python", "golang", "c++"}};
    printStudent2(&stu1);
}

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2022-08-19 18:45:50  更:2022-08-19 18:49:13 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/13 5:57:20-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码