C++ 结构与联合

本文详细介绍了C++中结构体的定义与使用,包括结构体的两种定义方法、变量的三种声明方法,结构体成员的访问,结构体的自引用与交叉引用,以及不完整声明的技巧。

1.结构体的两种定义方法和变量的三种声明方法:

#include <iostream>
using namespace std;

/* 结构体的两种定义方式 */
struct test01{
    int a;
    double b;
    float c;
};

typedef struct {
    int a ;
    double b;
    float c;
} test02;

int main(int argc, char* argv[]){
    /* 结构体变量的三种声明方式 */
    struct test01 t;    /* 需要struct标签 */
    test02 t1;          /* 不需要标签 */

    struct {
        int a;
        double b;
        float c;
    } t2;
    return 0;
}

2.结构体成员的访问

#include <iostream>

using namespace std;

typedef struct {
    int a;
    double b[10];
    float c;
}test_t;


int main(int argc, char* argv[]){

    test_t t;
    t.a = 1;     /* 结构体使用.访问成员 */

    test_t *t1;
    t1->a = 1;   /* 对于指针类型的结构体使用->访问成员 */
    (*t1).a =1;  /* *运算符优先级低于.,所以需要加括号 */

    return 0;
}

3.结构体的自引用

#include <iostream>

// struct test_t{
//     int a;
//     double b[10];
//     float c;
//     struct test_t t;       /* 在结构体中自己的变量是不合法的,因为这个操作会引起无穷的递归 */
// };

struct test_t
{
    int a;
    double b[10];
    float c;
    struct test_t *t; /* 此处是合法的,因为t只是一个指针,不会引起无穷递归 */
};

typedef struct
{
    int a;
    double b[10];
    float c;
    test01_t *t; /* 这是一个陷阱, 编译器解释到这一行时,test01_t还没有被定义 */
} test01_t;

/* 正确的做法如下 */
typedef struct test02_tag
{
    int a;
    double b[10];
    float c;
    struct test02_tag *t;
} test02_t;

4.结构体的交叉引用,不完整声明

#include <iostream>

using namespace std;

/* 如果存在两个结构体 A 和 B,它们相互引用,应该如何定义呢? 或者说先定义谁呢?*/
struct B;  /* 不完整定义 */

struct A
{
    int a;
    double b[10];
    float c;
    struct B *b;
};

struct B
{
    struct A *a;
};

int main(int argc, char *argv[])
{

    return 0;
}

 

转载于:https://www.cnblogs.com/PPWEI/p/11430631.html

### C++ 结构体 (struct) 和联合体 (union) 的完整代码示例 以下是关于 C++ 中 `struct` 和 `union` 的完整代码示例,展示了如何定义和使用这两种数据类型。 #### 使用结构体 (`struct`) 的示例 结构体是一种用户自定义的数据类型,允许将不同类型的数据组合在一起。下面是一个简单的例子: ```cpp #include <iostream> using namespace std; // 定义一个结构体 struct Person { string name; int age; float height; }; int main() { // 创建结构体实例并初始化 Person person1 = {"Alice", 30, 165.5}; cout << "Name: " << person1.name << endl; cout << "Age: " << person1.age << endl; cout << "Height: " << person1.height << " cm" << endl; return 0; } ``` 在这个例子中,我们创建了一个名为 `Person` 的结构体,并为其成员变量赋值[^1]。 --- #### 使用联合体 (`union`) 的示例 联合体也是一种复合数据类型,但它结构体不同的是,它共享同一块内存区域来存储不同的数据类型。这意味着,在任何时刻,只有一个成员可以保存有效值。 ```cpp #include <iostream> using namespace std; // 定义一个联合体 union Data { int i; float f; char str[20]; }; int main() { Data data; data.i = 10; cout << "Integer Value: " << data.i << endl; data.f = 220.5; cout << "Float Value: " << data.f << endl; strcpy(data.str, "C++"); cout << "String Value: " << data.str << endl; // 注意:访问其他字段可能会得到未定义的结果 cout << "Accessing Integer after String assignment: " << data.i << endl; return 0; } ``` 在此示例中,联合体 `Data` 被用来展示其特性——多个成员共享相同的内存位置[^1]。 --- #### 同时使用结构体和联合体的综合示例 有时可以在同一个程序中同时使用结构体和联合体。例如: ```cpp #include <iostream> #include <cstring> using namespace std; struct Record { int id; union { float score; char grade; }; }; int main() { Record record1; record1.id = 1; record1.score = 95.0f; cout << "Record ID: " << record1.id << ", Score: " << record1.score << endl; Record record2; record2.id = 2; record2.grade = 'A'; cout << "Record ID: " << record2.id << ", Grade: " << record2.grade << endl; return 0; } ``` 此代码片段演示了在一个结构体内嵌入联合体的情况,从而实现灵活的数据表示方式[^1]。 --- ### 总结 以上分别介绍了单独使用 `struct` 和 `union` 的方法以及两者结合使用的场景。通过这些示例可以看出,C++ 提供了强大的工具支持复杂数据类型的构建,同时也强调了类型安全性的重要性[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值