如果有人能详细解释一下,这两个声明有什么不同:
typedef struct atom {
int element;
struct atom *next;
};和
typedef struct {
int element;
struct atom *next;
} atom;发布于 2013-09-15 05:47:51
typedef的目的是为类型规范命名。语法为:
typedef <specification> <name>;完成这些操作之后,您可以使用<name>来声明变量,就像使用该语言的任何内置类型一样。
在您的第一个示例中,<specification>是以struct atom开头的所有内容,但后面没有<name>。因此,您还没有为类型规范指定一个新名称。
在struct声明中使用名称与定义新类型不同。如果要使用该名称,则必须始终在其前面加上struct关键字。所以如果你声明:
struct atom {
...
};您可以使用以下命令声明新变量:
struct atom my_atom;但你不能简单地声明
atom my_atom;对于后者,您必须使用typedef。
请注意,这是C和C++之间的显著区别之一。在C++中,声明struct或class类型确实允许您在变量声明中使用它,您不需要typedef。对于其他复杂类型构造,typedef在C++中仍然很有用,比如函数指针。
您可能应该查看与相关的侧栏中的一些问题,它们解释了该主题的其他一些细微差别。
发布于 2013-09-15 05:46:07
这是正常的structure declaration
struct atom {
int element;
struct atom *next;
}; //just declaration创建object
struct atom object; struct atom {
int element;
struct atom *next;
}object; //creation of object along with structure declaration和
这是struct atom类型的类型定义
typedef struct atom {
int element;
struct atom *next;
}atom_t; //creating new type其中,atom_t是struct atom的别名
对象的创建
atom_t object;
struct atom object; //both the ways are allowed and same发布于 2016-05-06 14:49:01
typedef关键字的一般语法为:typedef existing_data_type new_data_type;
typedef struct Record {
char ename[30];
int ssn;
int deptno;
} employee;https://stackoverflow.com/questions/18806392
复制相似问题