如果我创建了一个结构:结构和类型定义使用C
struct student {
int id;
char name [20];
student * next};
是什么意思:
typedef struct student student?
什么student * next
?
谢谢!
如果我创建了一个结构:结构和类型定义使用C
struct student {
int id;
char name [20];
student * next};
是什么意思:
typedef struct student student?
什么student * next
?
谢谢!
A typedef
创建一个新名称。因此,这些是完全等效的:在你的例子中,struct和typedef具有相同的名称。但它可能是这样的:
struct something {
/* ... */
};
typedef struct something student;
typedef的声明看起来像它引入的类型。
作为一面注,things are different in C++。
在C中,无论何时使用结构,都必须使用关键字struct
。通过输入typedef struct student student
,你可以简单地说说student
。
使用命名结构,关键字struct
通常用于创建结构实例并引用结构类型。
的typedef
让你几乎创建一个别名为student
,而不必使用struct student
不断,从而真正使得它更像一个内置的类型。
typedef struct student student;
这创造struct student
别名名称为student
。
结构标签和类型名称存在于C中的两个不同名称空间中(C++中不是这样),所以允许为别名和标签使用相同的名称。
你有没有考虑阅读[介绍性书](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list)? – 2012-02-07 14:21:27