C语言的高级东东——联合体、枚举、位字段

同一类事物,不同的数据类型,可以采用联合体来表示,如描述“量”这样东西,即可以用个数,也可以用重量,还可以用容积,这些都是量,如果用结构体来描述这三样东西,就会在存储器占用更多空间。解决办法就是用联合体,定义一个叫“量”的数据类型,然后根据特定数据决定要保存个数,重量还是容积,联合体的定义如下

union quantity{
        short count;
        float weight;
        float volumna;
};

如何使用联合体呢?有三种方式:C89方式、指定初始化器、“点”表示法:

int main(){
		//C89方式
        union quantity q ={4};
        //指定初始化器
        union quantity q1 = {.volumn = 6.0};
        //“点”表示法
        union quantity q2;
        q2.weight = 80;
        return 0;
}

其中C89方式,只会保存第一个字段的值。指定初始化器是按名设置联合体字段的值,结构体也可以按名设置字段值

但无论用哪种方法设置联合体的值,都只会保存一条数据,而联合体的大小最决于最长的字段,因为这样才能保证它足够大,任何一个字段都能装得下。

联合体通常会和结构体一起使用:

#include <stdio.h>

union quantity{
        short count;
        float weight;
        float volumn;
};
struct fruit{
        const char *name;
        float price;
        union quantity amount;
};

int main(){
        struct fruit pear = {.name = "Pear",.price = 15.0,.amount.weight = 1.0};
        printf("fruit:%s  price %.2f¥ weight %.2f kg\n",pear.name,pear.price,pear.amount.weight);
        return 0;

}

编译运行:

~/Desktop/MyC$ gcc test.c -o test
~/Desktop/MyC$ ./test
fruit:Pear  price 15.00¥ weight 1.00 kg

联合体使用会有一个问题:那就是在联合体保存各种可能的值后,就无法知道它的类型了。因为编译器不会记录你在联合体中设置或读过哪些字段的。这时我们可以通过一个技巧来解决,就是创建枚举。

枚举就是当你不想保存数字或文本,而只是想保存一组符号时,如MONDAY、TUESDAY、WEDNESDAY…这些符号时使用的。这也是为什么要发明枚举的原因。

定义枚举要用关键字enum,我们定义一个吧:

enum measure{
        COUNT,WEIGHT,VOLUMN
}; 

计算机会为列表中的每个符号分配一个数字,枚举变量中也只保存数字。至于是哪个数字,就不用操心了,C程序员只要在代码中写符号就行了。

我们在结构体fruit中加入枚举变量,就样一来,只要枚举变量与联合体中的类型对应上,在后续程序中就可以根据枚举变量的值,从而知道联合体变量的类型了。

struct fruit{
        const char *name;
        float price;
        union quantity amount;
        enum measure type;
};

我们看看完整的代码:

#include <stdio.h>

union quantity{
        short count;
        float weight;
        float volumn;
};
enum measure{
        COUNT,WEIGHT,VOLUMN
};
struct fruit{
        const char *name;
        float price;
        union quantity amount;
        enum measure type;  //加入枚举变量
};

int main(){
        struct fruit pear = {.name = "Pear",.price = 15.0,.amount.weight = 1.0,WEIGHT};
        switch(pear.type){

        case WEIGHT:
        case VOLUMN:
        printf("fruit:%s  price %.2f¥ weight %.2f kg\n",pear.name,pear.price,pear.amount.weight);
        break;
        case COUNT:

        printf("hello int");
        break;
        }
        return 0;

}

编译运行:

~/Desktop/MyC$ gcc test.c -o test
~/Desktop/MyC$ ./test
fruit:Pear  price 15.00¥ weight 1.00 kg

有了这个技巧就不怕不知道联合体的值的类型了。

最后介绍一下位字段

假设我们有这样一个结构体

struct asyth{
        short sequence;
        short from;
        short out_of_door;
        short success;
};

它的每个字段分别用0和1代码两种不能的状态。上面这个结构体完全可以胜任,就是会浪费很多空间。如果每个字段都用一个位来保存就好了,那么位字段就派上用场了:

struct asyth{
        short sequence:1;
        short from:1;
        short out_of_door:1;
        short success:1;
};

这样就可以用四个位来存储这些值了,很节省空间,所以位字段的用法就是在变量后面加上“:”和想要的比特位数。

猜你喜欢

转载自blog.csdn.net/weixin_40763897/article/details/87276712
今日推荐