Pointers on C——10 Structures and Unions.2

来源:互联网 发布:淘宝怎么更改实名认证 编辑:程序博客网 时间:2024/05/17 08:16

10.1.1 Structure Declarations

Structures are declared by listing the members that they will contain. This list includes the type and the name of each member.

在声明结构时,必须列出它包含的所有成员。这个列表包括每个成员的类型和名字。


struct tag

 { 

member-list

} variable-list ;


This structure declaration syntax requires some explanation. The optional fields cannot all be omitted—at least two of them must appear.

结构声明的语法需要作一些解释。所有可选部分不能全部省略——它们至少要出现两个。


Here are several examples.

这里有几个例子。


struct { 

int a;

char b;

float c;

} x;


This declaration creates a single variable named x, which contains three members: an integer, a character and a float.

这个声明创建了一个名叫x 的变量,它包含三个成员:一个整数、一个字符和一个浮点数。


struct {

int a;

char b;

float c;

} y[20], *z;


This declaration creates an array y of twenty structures and z, which is a pointer to a structure of this type.

这个声明创建了y 和z。 y 是一个数组,它包含了20 个结构。z 是一个指针,它指向这个类型的结构。


These two declarations are treated by the compiler as entirely different types, even though their member lists are identical. Thus, the variables y and z are a different type than x, so the statement

这两个声明被编译器当作两种截然不同的类型,即使它们的成员列表完全相同。因此,变量y和z 的类型和x 的类型不同,所以下面这条语句是非法的。


z = &x;


is illegal. But does this fact mean that ail structures of a given type must be created in a single declaration?

但是,这是不是意味着某种特定类型的所有结构都必须使用一个单独的声明来创建呢?


Fortunately, no. The tag field allows a name to be given to the member list so that it can be referenced in subsequent declarations. The tag allows many declarations to use the same member list and thus create structures of the same type. Here is an example.

幸运的是,事实并非如此。标签(tag)字段允许为成员列表提供一个名字,这样它就可以在后续的声明中使用。标签允许多个声明使用同一个成员列表,并且创建同一种类型的结构。这里有个例子。


struct SIMPLE {

int a;

char b;

float c;

};


The declaration associates the tag SIMPLE with this member list. The declaration doesnʹt have a variable list, so it doesnʹt create any variables.

这个声明把标签SIMPLE 和这个成员列表联系在一起。该声明并没有提供变量列表,所以它并未创建任何变量。


This declaration is similar to making a cookie cutter. A cookie cutter determines the shape of cookies yet to be made, but the cookie cutter is not a cookie. The tag identifies a pattern for declaring future variables, but neither the tag nor the pattern are variables.

这个声明类似于制造一个甜饼切割器。甜饼切割器决定制造出来的甜饼的形状,但甜饼切割器本身却不是甜饼。标签标识了一种模式,用于声明未来的变量,但无论是标签还是模式本身都不是变量。


struct SIMPLE x;

struct SIMPLE y[20], *z;


These declarations use the tag to create variables. They create the same variables as the first two examples but with one important difference—now x, y, and z are all the same kind of structure.Another good technique for declaring structures is to create a new type with a typedef, as in the following example.

这些声明使用标签来创建变量。它们创建和最初两个例子一样的变量,但存在一个重要的区别——现在x , y 和z 都是同一种类型的结构变量。声明结构时可以使用的另一种良好技巧是用typedef 创建一种新的类型,如下面的例子所示。


typedef struct {

int a;

char b;

float c;

} Simple;


This technique has almost the same effect as declaring a structure tag. The difference is that Simple is now type name rather than a structure tag, so subsequent declarations would look like this one.

这个技巧和声明一个结构标签的效果几乎相同。区别在于Simple 现在是个类型名而不是个结构标签,所以后续的声明可能像下面这个样子:


Simple x;

Simple y[20], *z;


If you want to use a particular structure in more than one source file, you should put the tag declaration or typedef in a header file. You can then #include the declaration wherever it is needed.

如果你想在多个源文件中使用同一种类型的结构,你应该把标签声明或typedef 形式的声明放在一个头文件中。当源文件需要这个声明时可以使用#include 指令把那个头文件包含进来。

上一章 Pointers on C——10 Structures and Unions.1

原创粉丝点击