简体   繁体   English

如果结构开头的名称和结构结尾的名称不同,那意味着什么?

[英]What does it mean when the name at the beginning of the struct and the one at the end of the struct are different?

What does it mean when the name at the beginning of the struct and the one at the end of the struct are different? 如果结构开头的名称和结构结尾的名称不同,那意味着什么? For example: 例如:

struct book{
   char title[50];
   int year;
}boo;

or for example 或者例如

typedef struct book{
    char title[50];
    int year;
}boo;

This example 这个例子

struct book{
   char title[50];
   int year;
}boo;

Creates a variable called boo that has the type struct book . 创建一个名为boo的变量,其类型为struct book

The other example: 另一个例子:

typedef struct book{
    char title[50];
    int year;
}boo;

Defines boo to be the same type as struct book , an alias sort-of. boo定义为与struct book相同的类型,别名为sort-of。

In the 1st case you define a struct and right away create a variable of its type. 在第一种情况下,您将定义一个结构并立即创建其类型的变量。

struct book{
    char title[50];
    int year;
}boo; // <== boo is already a variable, you can start using it; boo.year = 2019;

In the second example you make a typedef saying that the 'boo' declaration is now the same as your struct so you can create variables using that 'boo' afterwards. 在第二个示例中,您键入了一个typedef,说明'boo'声明现在与您的结构相同,因此您可以在以后使用该'boo'创建变量。 In this case no variable is created at the time of the struct declaration. 在这种情况下,在结构声明时不会创建任何变量。

typedef struct book{
    char title[50];
    int year;
}boo;

boo a, b; // <== here you create variables

struct book is the type name (like int or double ). struct book类型名称 (如intdouble )。 The type is being defined by the stuff between the curly braces - { char title[50]; int year; } 该类型由花括号之间的内容定义- { char title[50]; int year; } { char title[50]; int year; } { char title[50]; int year; } . { char title[50]; int year; } In the first snippet, boo is being declared as an object (variable) of type struct book . 在第一个代码段中, boo被声明为struct book类型的对象 (变量)。

C allows you to both define the struct type and declare objects of that type in the same declaration. C允许您定义结构类型并在同一声明中声明该类型的对象。 It may make sense to see the two operations broken up: 看到两个操作分开可能是有道理的:

struct book { char title[50]; int year; }; // creates the *type* "struct book"
struct book boo;                           // declares a *variable* of type "struct book"

The typedef facility allows you to create synonyms or aliases for a type - in this case, typedef struct book { char title[50]; int year; } boo; 使用typedef工具可以为类型创建同义词或别名-在这种情况下,请使用typedef struct book { char title[50]; int year; } boo; typedef struct book { char title[50]; int year; } boo; creates boo as a synonym for struct book . 创建boo作为struct book的同义词。 You can then create objects as 然后,您可以创建对象

boo b1; // b1 is type boo, which is a synonym for struct book.

Again, it may help to split things up: 同样,这可能有助于将事情分解:

struct book { char title[50]; int year; }; // creates the type "struct book"
typedef struct book boo;                   // makes boo an alias for type "struct book"

In struct book , book is the tag name for the struct type - it's what allows you to refer to that type after you've defined it, like struct bookbook是struct类型的标记名 -定义类型后,便可以引用该类型,例如

struct book b2;

void some_function( struct book b );

etc. 等等

If you write something like 如果你写类似

struct { char title[50]; int year; } boo;

then only boo can have that type - you can't declare other variables of that same type, because there's no way to refer to it anymore. 那么只有 boo可以具有该类型-您无法声明同一类型的其他变量,因为无法再引用它。 Even if you repeat the type: 即使您重复输入:

 struct { char title[50]; int year; } boo;
 struct { char title[50]; int year; } b2;

boo and b2 technically have different types, even though the type implementations are identical. boob2技术上具有不同的类型,即使类型实现是相同的。

Now, if you use the typedef facility, you can omit the tag name: 现在,如果您使用typedef工具,则可以省略标签名称:

typedef struct { char title[50]; int year } boo;

because now you refer to that type with the typedef name boo : 因为现在您使用typedef名称 boo来引用该类型:

boo b2;

void some_function( boo b );

When an identifier (a name) appears after struct , it is called a tag and the combined struct and identifier name a type. 当标识符(名称)出现在struct之后时,它称为标记 ,而组合的struct和identifier则将其命名为类型。 Quite simply, struct book is a type; 简而言之, struct book是一种类型。 its name is struct book . 它的名字叫struct book

When an identifier appears after the braces of a structure declaration, it is a separate name—it does not have an immediate connection with the structure. 当标识符出现在结构声明的花括号后面时,它是一个单独的名称,它与结构没有直接联系。 Its role depends on the larger declaration it is in. To see this, consider that one form of a declaration is “ type name ; 它的作用取决于它所在的较大的声明。为此,请考虑声明的一种形式是“ 类型 ; ”. ”。 For example, we can have: 例如,我们可以有:

int a;
char b;
struct foo c;
struct bar { float x, y, z; } d;

In each of these, a , b , c , and d is an identifier for an object being declared, and the text preceding it is a type name. 在每种情况下, abcd是要声明的对象的标识符,其前面的文本是类型名称。 struct foo and struct bar { float x, y, z } are merely type names that are complicated instead of being single words. struct foostruct bar { float x, y, z }只是复杂的类型名称,而不是单个单词。 When declaring an identifier, we can use any name we want (following the usual rules about which characters are used and about reserved names and such). 声明标识符时,我们可以使用所需的任何名称(遵循有关使用哪些字符以及保留名称等的常规规则)。

We can also have various modifiers before the type name, such as: 我们还可以在类型名称之前添加各种修饰符,例如:

extern int a;
static char b;
const struct foo c;
typedef struct bar { float x, y, z; } d;

The first three above also declare objects. 上面的前三个还声明了对象。 In the fourth, typedef is special. 第四, typedef是特殊的。 It says “This is not declaring an object; 它说:“这不是声明对象; it is making a new name for a type.” The fourth declaration says “d” is a new name for struct bar . 第四个声明说“ d”是struct bar的新名称。

It is common to use typedef to make an alias for a struct that has the same name as the structure tag, as in: 通常使用typedef为与结构标签同名的struct创建别名,如下所示:

typedef struct bar { float x, y, z; } bar;

However, there is no requirement for this in C. The name of the structure and any aliases for its type are unrelated, unless a programmer writes a typedef making them the same. 但是,在C语言中对此没有要求。除非程序员编写使它们相同的typedef ,否则结构的名称及其类型别名是不相关的。 (In C++, this is done automatically; declaring a struct bar creates a type named bar . In C, structure tags are in a different name space than type names.) (在C ++中,这是自动完成的;声明struct bar会创建一个名为bar的类型。在C中,结构标签与类型名位于不同的名称空间中。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM