正如标题所说,我有这样的代码:C:typedef struct name {...}; VS typedef struct {...}名称;
typedef struct Book{
int id;
char title[256];
char summary[2048];
int numberOfAuthors;
struct Author *authors;
};
typedef struct Author{
char firstName[56];
char lastName[56];
};
typedef struct Books{
struct Book *arr;
int numberOfBooks;
};
我得到这些错误来自海湾合作委员会:
bookstore.c:8:2: error: unknown type name ‘Author’
bookstore.c:9:1: warning: useless storage class specifier in empty declaration [enabled by default]
bookstore.c:15:1: warning: useless storage class specifier in empty declaration [enabled by default]
bookstore.c:21:2: error: unknown type name ‘Book’
bookstore.c:23:1: warning: useless storage class specifier in empty declaration [enabled by default]
如果我改变类型定义是这样的:
typedef struct{
char firstName[56];
char lastName[56];
} Author;
然后没有警告并没有发生错误。通过搜索通过http://www.amazon.com/C-Programming-Language-2nd-Edition/dp/0131103628和几个小时的谷歌搜索我不明白为什么第一个实施将无法正常工作。
在'Book'之前移动'Author'。还要注意你的'typedef'是多余的 – SomeWittyUsername
在'Author'结构中改变'error:unknown type name'Book''怎么可能呢?请看看[这里](http://msdn.microsoft.com/en-us/library/4x7sfztk%28v=vs.80%29.aspx),它清楚地提到了typdef结构和定义结构之间的区别。 –