C语言中的struct分析

一:

C语言中的struct可以看作变量的集合

example:——————————————————————————————

空结构体占多大内存?

struct TS                  ---------------------------------->          sizeof(struct TS) = ?

{

};

——————————————————————————————————

二:

C语言中的struct分析

C语言中的struct分析

三:

结构体与柔性数组

—— C语言中可以由结构图产生柔性数组

—— C语言中结构体的最后一个元素可以使大小未知的数组

example:———————————————————————————————————————

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

            {

                        int len;

                        int array[];

            };

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

———————————————————————————————————————————

四:

C语言中的struct分析

C语言中的struct分析

五:

柔性数组的用法:———————————————————————————————————

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

            {

                        int len;

                        int array[];

            };

            struct SoftArray* sa = NULL;

            sa = (struct SoftArray*)malloc(sizeof(struct SoftArray) + sizeof(int) * 5);

            sa->len = 5;

C语言中的struct分析

———————————————————————————————————————————

六:

C语言中的struct分析

C语言中的struct分析

C语言中的struct分析