【发布时间】:2015-08-25 02:37:04
【问题描述】:
我在学习数据结构课程,这让我很困惑。
我猜它与指针属性有关,但在我的研究中我没有找到任何真正的解释,知道为什么 C 允许这样做吗?
可运行代码:http://ideone.com/kgh3LF
#include <stdio.h>
#include <stdlib.h>
/* Declaring a typedef struct */
typedef struct{
int a;
char b[10];
}struct_one;
/* Declaring another structure, with an intentional wrong calling of the first structure */
struct struct_two{
int p;
char q[10];
/* This doesn't work as expected... should be: struct_one var; */
// struct struct_one var;
/* THIS ONE DOES WORK!!, and i'm not sure why */
struct struct_one *ptr;
};
int main(void) {
/* code */
return 0;
}
【问题讨论】:
-
在声明指向相同的指针时,您正在隐式前向声明
struct struct_one类型。如果您想看到它吹出大块,请尝试在main()中尝试struct two obj; obj.ptr = malloc(sizeof *obj.ptr);,从而将significant doubt 借给“这个确实有效!” -
这就是为什么我不同意'common'(?)智慧不 typedef 你的结构。 typedef 名称中的拼写错误会被编译器捕获,但结构标记中的拼写错误可能不会,这取决于它的使用方式。
标签: c pointers data-structures typedef