C 语言--sizeof与typedef

来源:互联网 发布:2017年淘宝双十一 编辑:程序博客网 时间:2024/06/10 00:05

sizeof 与 typedef

sizeof作为C语言中求取类型所占字节数的宏,经常配合malloc等使用。
typedef则是用来表明,类型别名。

typedef struct ListElmt_ {    void *data;    struct ListElmt_ *next;}ListElmt;

通过使用typedef,可以直接用ListElmt来代替struct ListElmt结构体类型。

typedef struct List_ {    int size;    ListElmt *head;    ListElmt *tail;    void (*destroy)(void *data);    int (*match)(const void *key1, const void *key2); } List;

在使用链表时,可以使用malloc函数

List *list = (List *)malloc(sizeof(List));list_init(list);

在链表中,每个链表元素包含的数据都是数据的地址,这样,由于对应不同的类型,如何销毁元素就需要自定义List中的destroy函数,如果需要排序比较,也需要同时定义match函数。

0 0