C:Why 中的结构不显示与 fied fe 不兼容的类型;
Structures in C:Why this does not show incompatible type for fied fe;
有人可以解释一下下面给出的 c 结构的前 2 个案例吗?它是如何工作的?
案例:1
struct vertex{
int info;
struct vertex * nextertex;
struct edge *firstEdge;}; //why no error as incompatible type for field firstEdge?
这段代码不会产生错误!我们在整个代码中没有struct edge!!
案例:2
struct vertex{
int info;
struct vertex * nextertex;
struct edge *firstEdge;};
struct edge{
struct vertex * destVertex;
struct edge *nextEdge;};
与case:1相同,不同之处在于我们在结构顶点定义之后有结构边缘。没有错误!
案例:3
struct vertex{
int info;
struct vertex * nextVertex;
struct edge firstEdge;};
struct edge{
struct vertex * destVertex;
struct edge *nextEdge;};
这似乎更多 obvious.Throws 错误,因为结构 v.
中字段 firstEdge 的类型不兼容
用 struct edge *firstEdge 替换 struct edge firstEdge 消除错误或先定义边再顶点。
在前两种情况下,您定义了指向某个结构[struct edge *
] 的指针,因此该定义是有效的。 struct edge
的定义不需要在 struct vertex
定义之前。
在第三种情况下,您正在定义结构的变量。为此,编译器需要在使用其他结构之前对其进行定义。
C 具有 完整 和 不完整 类型的概念。
C 表示不完整类型缺少足够的信息来确定该类型对象的大小,而完整类型提供
足够的信息。
C 不允许您使用不完整类型的成员来声明结构类型,但您可以使用指向不完整类型的类型指针的成员来声明结构类型。在后一种情况下,如果类型尚未完成(通过稍后声明结构类型及其定义内容,则尝试取消引用指向不完整类型的指针是无效的在同一范围内).
有人可以解释一下下面给出的 c 结构的前 2 个案例吗?它是如何工作的?
案例:1
struct vertex{
int info;
struct vertex * nextertex;
struct edge *firstEdge;}; //why no error as incompatible type for field firstEdge?
这段代码不会产生错误!我们在整个代码中没有struct edge!!
案例:2
struct vertex{
int info;
struct vertex * nextertex;
struct edge *firstEdge;};
struct edge{
struct vertex * destVertex;
struct edge *nextEdge;};
与case:1相同,不同之处在于我们在结构顶点定义之后有结构边缘。没有错误!
案例:3
struct vertex{
int info;
struct vertex * nextVertex;
struct edge firstEdge;};
struct edge{
struct vertex * destVertex;
struct edge *nextEdge;};
这似乎更多 obvious.Throws 错误,因为结构 v.
中字段 firstEdge 的类型不兼容用 struct edge *firstEdge 替换 struct edge firstEdge 消除错误或先定义边再顶点。
在前两种情况下,您定义了指向某个结构[struct edge *
] 的指针,因此该定义是有效的。 struct edge
的定义不需要在 struct vertex
定义之前。
在第三种情况下,您正在定义结构的变量。为此,编译器需要在使用其他结构之前对其进行定义。
C 具有 完整 和 不完整 类型的概念。
C 表示不完整类型缺少足够的信息来确定该类型对象的大小,而完整类型提供 足够的信息。
C 不允许您使用不完整类型的成员来声明结构类型,但您可以使用指向不完整类型的类型指针的成员来声明结构类型。在后一种情况下,如果类型尚未完成(通过稍后声明结构类型及其定义内容,则尝试取消引用指向不完整类型的指针是无效的在同一范围内).