读书人

结构体中的继承?解决思路

发布时间: 2012-03-13 11:21:11 作者: rapoo

结构体中的继承?
typedef struct node {
struct node *next;
} node;

typedef struct work_node {
struct node *next;
int jobnum;
} wnode;

wnode *mywork = (wnode *) queue_get(&queue);
~~~~~~~~~~~~~~~这样的使用有什么道理?

queue_get() 从 queue 中取一个 node *
queue的定义如下
typedef struct queue {
node *head, *tail;
} queue;

出处:http://www-128.ibm.com/developerworks/linux/library/l-posix3/

p.s. 组织问题的时候,又考虑了下,明白为何这样用了,不过始终觉得这种方式很诡异,不知道这是不是一种比较常见的技巧...?

[解决办法]
比较常见

struct xx
{
int len;
char aa[1];
}
用做变长数组
xx*p = (xx*)malloc(sizeof(xx)+1024);
这样p-> aa就有1028大小的空间了。

读书人网 >C语言

热点推荐