C语言进阶剖析 10 struct 和 union

版权声明:本文为博主原创文章,转载请注明地址【http://blog.csdn.net/czg13548930186】 谢谢! https://blog.csdn.net/czg13548930186/article/details/86359318

struct 的小秘密

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

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

#include <stdio.h>

struct TS
{

}ts0;


int main()
{

struct TS
{

}ts1;
	
    struct TS ts2;
    struct TS ts3;
    
    printf("sizeof(ts0) = %d,  &ts0 = %p\n", sizeof(ts0),&ts0);
    printf("sizeof(ts0) = %d,  &ts0 = %p\n", sizeof(ts1),&ts1);
    printf("sizeof(ts0) = %d,  &ts0 = %p\n", sizeof(ts2),&ts2);
    printf("sizeof(ts0) = %d,  &ts0 = %p\n", sizeof(ts3),&ts3);

    return 0;
}
输出:【GCC】
book@book-desktop:~$ ./a.out
sizeof(ts0) = 0,  &ts0 = 0x804a01c
sizeof(ts0) = 0,  &ts0 = 0xbf910950
sizeof(ts0) = 0,  &ts0 = 0xbf910950
sizeof(ts0) = 0,  &ts0 = 0xbf910950

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

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


结构体与柔性数

  • 柔性数组的用法
#include <stdio.h>
#include <malloc.h>
struct SoftArray
{
    int len;
    int array[];  // 当且仅当作为结构体的最后一个元素时可以使用
};
// ...

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

printf("%d\n", sizeof(struct SoftArray));  // 4
}

在这里插入图片描述

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

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

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

struct SoftArray* creat_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( sa != NULL )
    {
        for(i=0; i<sa->len; i++)
        {
            sa->array[i] = i + 1;
        }
    }
}

int main()
{
    int i = 0;
    struct SoftArray* sa = creat_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 的使用受系统大小端的影响
        ○ 小端,低字节在低地址
        ○ 大端,低字节在高地址
    在这里插入图片描述
void code()
{
    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;
    }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 的使用会受到系统大小端的一影响

猜你喜欢

转载自blog.csdn.net/czg13548930186/article/details/86359318