C:typedef结构名称{...}; VS typedef结构{...}名; [英] C : typedef struct name {...}; VS typedef struct{...} name;

查看:631
本文介绍了C:typedef结构名称{...}; VS typedef结构{...}名;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为标题所说,我有这个code:

As title says, i have this code :

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;
};

我得到这些错误来自海湾合作委员会:

I get these errors from gcc :

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]

如果我改变类型定义是这样的:

If i change the typedefs like this :

typedef struct{
    char firstName[56];
    char lastName[56];
} Author;

然后没有警告也没有错误发生。通过<一说完搜索href=\"http://rads.stackoverflow.com/amzn/click/0131103628\">http://www.amazon.com/C-Programming-Language-2nd-Edition/dp/0131103628和几个小时的谷歌搜索我想不通,为什么第一个实现将无法工作。

Then no warnings and no errors occur. Having searched through http://www.amazon.com/C-Programming-Language-2nd-Edition/dp/0131103628 and a couple of hours googling i can't figure why the first implementation won't work.

推荐答案

有几件事情怎么回事。首先,正如其他人所说,编译器的有关未知类型的投诉可能是因为你需要使用它们之前定义的类型。更重要的是,虽然是要了解三件事的语法:(1)结构的定义,(2)结构声明,以及(3)的typedef

There are several things going on here. First, as others have said, the compiler's complaint about unknown type may be because you need to define the types before using them. More important though is to understand the syntax of 3 things: (1) struct definition, (2) struct declaration, and (3) typedef.

当定义一个结构,该结构可以被命名,或无名(如果未命名,那么它必须立即使用(会解释这是什么意思下文))。

When Defining a struct, the struct can be named, or unnamed (if unnamed, then it must be used immediately (will explain what this means further below)).

struct Name {
   ...
};

这定义了一个名为结构名称类型,然后就可以用来声明结构变量:

This defines a type called "struct Name" which then can be used to Declare a struct variable:

struct Name myNameStruct;

这声明了一个名为 myNameStruct 变量,它是一个类型的结构结构名称

This declares a variable called myNameStruct which is a struct of type struct Name.

您还可以定义一个结构,并同时声明结构体变量:

You can also Define a struct, and declare a struct variable at the same time:

struct Name {
   ...
} myNameStruct;

和以前一样,这个声明了一个名为 myNameStruct 变量,它是一个类型的结构结构名称 ... < STRONG>但它确实是在它声明的类型<同一时间/ STRONG> 结构名称。结果
该类型可以再次用来声明另一个变量:

As before, this declares a variable called myNameStruct which is a struct of type struct Name ... But it does it at the same time it declares the type struct Name.
The type can be used again to declare another variable:

struct Name myOtherNameStruct;

现在的typedef只是一个别名具有特定名称类型的方式:

Now typedef is just a way to alias a type with a specific name:

typedef OldTypeName NewTypeName;

鉴于上述的typedef,您使用的任何时间 NewTypeName 这是与使用 OldTypeName 在C语言编程,这是与结构尤为有用,因为它可以让你声明的类型的变量的时候离开过这个词结构,并就简单地把结构体的名称作为一个类型的能力它自己(就像我们用C ++做)。下面是第一定义结构的一例,然后类型定义的结构:

Given the above typedef, any time you use NewTypeName it is the same as using OldTypeName. In the C programming language this is particularly useful with structs, because it gives you the ability to leave off the word "struct" when declaring variables of that type and to treat the struct's name simply as a type on its own (as we do in C++). Here is an example that first Defines the struct, and then typedefs the struct:

struct Name {
   ...
};

typedef struct Name Name_t;

在上面OldTypeName是结构名称和NewTypeName是 Name_t 。所以,现在,申报类型结构名称的变量,而不是写:

In the above OldTypeName is struct Name and NewTypeName is Name_t. So now, to declare a variable of type struct Name, instead of writing:

struct Name myNameStruct;

我可以简单的写:

I can simple write:

Name_t myNameStruct;

还要注意,typedef的可以合并的结构定义,这是你在code在做什么:

typedef struct {
   ...
} Name_t;

而命名的结构这也可以做到,但这是多余的:

This can also be done while naming the struct, but this is superfluous:

typedef struct Name {
   ...
} Name_t;

注意哦:在上面的语法,因为你已经开始用的typedef,那么整个语句是一个的typedef 语句,其中在OldTypeName恰好是一个结构定义。因此编译器间$ P $点的名字来的之后的右花括号}作为NewTypeName ...它是的的变量名(因为这将是在语法没有的typedef,在这种情况下,你会定义结构,并同时声明一个结构变量)。

NOTE WELL: In the syntax above, since you have started with "typedef" then the whole statement is a typedef statement, in which the OldTypeName happens to be a struct definition. Therefore the compiler interprets the name coming after the right curly brace } as the NewTypeName ... it is NOT the variable name (as it would be in the syntax without typedef, in which case you would be defining the struct and declaring a struct variable at the same time).

此外,如果你的状态类型定义,但离开落Name_t然后结束,那么你已经有效地创建一个不完整的typedef语句,因为编译器认为在一切结构名称{...} 作为OldTypeName,并且没有提供typedef的一个NewTypeName。这就是为什么编译器是不愉快的code,你写它(尽管编译器的消息是非常模糊的,因为它不是很确定你做错了什么)。

Furthermore, if you state typedef, but leave off the Name_t at then end, then you have effectively created an INCOMPLETE typedef statement, because the compiler considers everything within "struct Name { ... }" as OldTypeName, and you are not providing a NewTypeName for the typedef. This is why the compiler is not happy with the code as you have written it (although the compiler's messages are rather cryptic because it's not quite sure what you did wrong).

现在,正如我上面提到的,如果你不命名你定义它的时间结构类型,则必须使用它立即要么声明一个变量:

Now, as I noted above, if you do not name the struct type at the time you define it, then you must use it immediately either to declare a variable:

struct {
   ...
} myNameStruct;  // declares myNameStruct as a variable with this struct
                 // definition, but the definition cannot be re-used.

或者你也可以在typedef使用一个未命名的结构类型:

Or you can use an unnamed struct type in a typedef:

typedef struct {
   ...
} Name_t;

这最后的语法是当你写你实际做:

This final syntax is what you actually did when you wrote:

typedef struct{
   char firstName[56];
   char lastName[56];
} Author;

和编译器是幸福的。 HTH。

And the compiler was happy. HTH.

对于有关_t后缀评论/问题:

Regarding the comment/question about the _t suffix:

_t后缀是一个惯例,以表明人们阅读code,与_t符号名是一个类型名称(而不是一个变量名)。编译器不分析,也不是知道,_t的。

_t suffix is a convention, to indicate to people reading the code that the symbolic name with the _t is a Type name (as opposed to a variable name). The compiler does not parse, nor is it aware, of the _t.

的C89,特别是C99的标准库中定义多​​种类型和选择使用_t这些类型的名称。例如C89标准定义了wchar_t的,off_t,ptrdiff_t的。 C99标准定义了很多额外的类型(如uintptr_t),还会将intmax_t,中int8_t,uint_least16_t,uint_fast32_t等,但_t没有保留,也不是专门解析,也不是由编译器发现,它仅仅是一个约定好要遵循当你定义新类型(通过类型定义)的C.在C ++中很多人利用公约来以大写启动类型的名称,例如,MyNewType(相对于C约定my_new_type_t)。 HTH

The C89, and particularly the C99, standard libraries defined many types AND CHOSE TO USE the _t for the names of those types. For example C89 standard defines wchar_t, off_t, ptrdiff_t. The C99 standard defines a lot of extra types, such as uintptr_t, intmax_t, int8_t, uint_least16_t, uint_fast32_t, etc. But _t is not reserved, nor specially parsed, nor noticed by the compiler, it is merely a convention that is good to follow when you are defining new types (via typedef) in C. In C++ many people use the convention to start type names with an uppercase, for example, MyNewType ( as opposed to the C convention my_new_type_t ). HTH

这篇关于C:typedef结构名称{...}; VS typedef结构{...}名;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆