在利用typedef时,发现个奇怪的现象:
运行环境是code block
案例1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Vnode *ad;
struct Gnode
{
int nv;
int ne;
ad G[MAX];
};
struct Vnode
{
int x;
};
int main(void)
{
return 0;
}
没有报错,允许使用一个未定义的Vnode。
案例2
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Vnode ad;
struct Gnode
{
int nv;
int ne;
ad G[MAX];
};
struct Vnode
{
int x;
};
int main(void)
{
return 0;
}
};
而这种清空下却不允许,提示error: array type has incomplete element type
下面代码调换顺序,先定义Vnode就没问题
案例3
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Vnode ad;
struct Vnode
{
int x;
};
struct Gnode
{
int nv;
int ne;
ad G[MAX];
};
int main(void)
{
return 0;
}
typedef是为一个已有类型起别名,那案例1为啥能成立?
求解,感激不尽。
因为你之前定义的是个指针哦,
typedef struct Vnode* ad;
指针类型是确定的,是个整型,这里你下面即使没有定义struct Vnode, 编译也是可以通过的,如果你要实例化Gnode时,那编译器只要在文件中找得到这个Vnode的定义就行,顺序就没有关系了。
你后面定义的不是一个指针,是个新增的类型,那编译器就要找到这个类型。这块详细的可在编译原理那门课里找到答案,我学得也不是很深,只能说到这了
案例2 在struct Gnode中使用了还没有定义的结构体struct Vnode,编译是从上往下按顺序编译的,这就是问什么main函数中的子函数如果定义在main函数前,主函数中就可以直接调用子函数,如果子函数写在主函数后,就必须在主函数前声明!!!