C语言 第10课 struct和union分析

struct 的小秘密

  • C 语言中的 srtuct 可以看作变量的集合
  • struct 的问题 : 结构体占用多大内存?
void code()
{
    struct TS
    {
    };
    
    printf("%d\n", sizof(struct TS));
}

实例分析: 空结构体的大小 

#include <stdio.h>

struct TS
{

};

int main()
{
    struct TS t1;
    struct TS t2;
    
    printf("sizeof(struct TS) = %d\n", sizeof(struct TS));
    printf("sizeof(t1) = %d, &t1 = %p\n", sizeof(t1), &t1);
    printf("sizeof(t2) = %d, &t2 = %p\n", sizeof(t2), &t2);
    
    return 0;
}
输出:[GCC]
sizeof(struct TS) = 0
sizeof(t1) = 0, &t1 = 0xbf910950
sizeof(t2) = 0, &t2 = 0xbf910950

C 语言的灰色地带:不同编译器有不同的行为

  • GCC 大小为 0
  • BCC, VC 报错

结构体与柔性数

  • 柔性数组即数组大小待定的数组
  • C语言可以由结构体产生柔性数组
  • C语言结构体的最后一个元素可以使大小未知的数组
struct SoftArray
{
    int len;
    int array[];
};

===>>>  sizeof(struct SoftArray) = ?

SoftArray 中的array仅是一个待用的标识符,不占用存储空间。

  • 柔性数组的用法
struct SoftArray
{
    int len;
    int array[];
};

// ...

struct SoftArray* sa = NULL;
sa = (struct SoftArray*)malloc(sizeof(struct SoftArray*) + sizeof(int) + 5);
sa->len = 5;

实例分析:柔性数组使用分析

#include <stdio.h>
#include <malloc.h>

struct SoftArray
{
    int len;
    int array[];
};

struct SoftArray* create_soft_array(int size)
{
    struct SoftArray* ret = NULL;
    
    if( size > 0 )
    {
        ret = (struct SoftArray*)malloc(sizeof(struct SoftArray) + sizeof(int) * size);
        
        ret->len = size;
    }
    
    return ret;
}

void delete_soft_array(struct SoftArray* sa)
{
    free(sa);
}

void func(struct SoftArray* sa)
{
    int i = 0;
    
    if( NULL != sa )
    {
        for(i=0; i<sa->len; i++)
        {
            sa->array[i] = i + 1;
        }
    } 
}

int main()
{
    int i = 0;
    struct SoftArray* sa = create_soft_array(10);
    
    func(sa);
    
    for(i=0; i<sa->len; i++)
    {
        printf("%d\n", sa->array[i]);
    }
    
    delete_soft_array(sa);
    
    return 0;
}
输出:
1
2
3
4
5
6
7
8
9
10

C语言中的 union

  • C 语言中的 union 在语法上与 struct 相似
  • union 只分配最大成员的空间,所有成员共享这个空间
void code_1()
{
    struct A
    {
        int a;
        int b;
        int c;
    }
    
    printf("%d\n", sizeof(struct A));  // 12
}

void code_2()
{
    union B
    {
        int a;
        int b;
        int c;
    }
    
    printf("%d\n", sizeof(union B));   // 4
}

union 的注意事项

  • union 的使用受系统大小端的影响
        ○ 小端,低字节在低地址
        ○ 大端,低字节在高地址

union C 
{ 
    int i; 
    char c; 
}; 

union C c; 
c.i = 1; 
printf("%d\n", c.c); // ?

编程实验: 判断系统的大小端

#include <stdio.h>

int system_mode()
{
    union SM
    {
        int i;
        char c;
    };

    union SM sm;
    
    sm.i = 1;
    
    return sm.c;
}


int main()
{
    printf("System Mode: %d\n", system_mode());
    return 0;
}
输出:低字节在低地址,小端
System Mode: 1

小结

  • strut 中的每个数据成员有独立的存储空间
  • strut 可以通过最后的数组标识符产生柔性数组
  • union 中的所有数据成员共享一个存储空间
  • union 的使用会受到系统大小端的一影响
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值