博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

C语言中typedef可以出现在struct定义之前

Posted on 2011-07-22 14:45  天地玄黄  阅读(741)  评论(0编辑  收藏  举报

      一直以为typedef必须在相应的数据类型之后才可定义,原来在前面也可以:

#include <stdio.h>
#include <stdlib.h>

/* the typedef is before the struct */
typedef struct pcap_if* pcap_if_p;

struct pcap_if {
	struct pcap_if *next;
	int a;
};

int main()
{
	const pcap_if_p a = (pcap_if_p)malloc(sizeof(struct pcap_if));
	a->a = 1;
	a->a = 2;
	printf("%d", a->a);

	return 0;
}