Chapter 6

来源:互联网 发布:汕头宝美工培训费用 编辑:程序博客网 时间:2024/06/05 10:26

A structure is a collection of one or more variables, possibly of different types, grouped together under a single name for convenient handling. (Structures are called ``records'' in some languages, notably Pascal.) Structures help to organize complicated data, particularly in large programs, because they permit a group of related variables to be treated as a unit instead of as separate entities.

结构是一个或多个变量的集合,这些变量可能为不同的类型,为了处理的方便而将这些变量组织在一个名字之下。(某些语言将结构称为“记录”,比如Pascal语言。)由于结构将一组相关的变量看作一个单元而不是各自独立的实体,因此结构有助于组织复杂的数据,特别是在大型的程序中。


One traditional example of a structure is the payroll record: an employee is described by a set of attributes such as name, address, social security number, salary, etc. Some of these in turn could be structures: a name has several components, as does an address and even a salary. Another example, more typical for C, comes from graphics: a point is a pair of coordinate, a rectangle is a pair of points, and so on.

工资记录是用来描述结构的一个传统例子。每个雇员由一组属性描述,如姓名、地址、社会保险号、工资等。其中的某些属性也可以是结构,例如姓名可以分成几部分,地址甚至工资也可能出现类似的情况。C语言中更典型的一个例子来自于图形领域:点由一对坐标定义,矩形由两个点定义,等等。


The main change made by the ANSI standard is to define structure assignment - structures may be copied and assigned to, passed to functions, and returned by functions. This has been supported by most compilers for many years, but the properties are now precisely defined. Automatic structures and arrays may now also be initialized.

ANSI标准在结构方面最主要的变化是定义了结构的赋值操作——结构可以拷贝、赋值、传递给函数,函数也可以返回结构类型的返回值。多年以前,这一操作就已经被大多数的编译器所支持,但是,直到这一标准才对其属性进行了精确定义。在ANSI标准中,自动结构和数组现在也可以进行初始化。


6.1 Basics of Structures

Let us create a few structures suitable for graphics. The basic object is a point, which we will assume has an x coordinate and a y coordinate, both integers.

我们首先来建立一些适用于图形领域的结构。点是最基本的对象,假定用坐标表示它,且xy的坐标值都为整数(参见图6-1


The two components can be placed in a structure declared like this:

struct point {

int x;

int y;

};


The keyword struct introduces a structure declaration, which is a list of declarations enclosed in braces. An optional name called a structure tag may follow the word struct (as with point here). The tag names this kind of structure, and can be used subsequently as a shorthand for the part of the declaration in braces.

关键字struct 引入结构声明。结构声明由包含在花括号内的一系列声明组成。关键字struct后面的名字是可选的,称为结构标记(这里是point)。结构标记用于为结构命名,在定义之后,结构标记就代表花括号内的声明,可以用它作为该声明的简写形式。


The variables named in a structure are called members. A structure member or tag and an ordinary (i.e., non-member) variable can have the same name without conflict, since they can always be distinguished by context. Furthermore, the same member names may occur in different structures, although as a matter of style one would normally use the same names only for closely related objects.

结构中定义的变量称为成员。结构成员、结构标记和普通变量(即非成员)可以采用相同的名字,它们之间不会冲突,因为通过上下文分析总可以对它们进行区分。另外,不同结构中的成员可以使用相同的名字,但是,从编程风格方面来说,通常只有密切相关的对象才会使用相同的名字。


A struct declaration defines a type. The right brace that terminates the list of members may be followed by a list of variables, just as for any basic type. That is,

struct声明定义了一种数据类型。在标志结构成员表结束的右花括号之后可以跟一个变量表,这与其它基本类型的变量声明是相同的。例如:


struct { ... } x, y, z;

is syntactically analogous to


int x, y, z;


in the sense that each statement declares x, y and z to be variables of the named type and causes space to be set aside for them.

这两个声明都将xyz声明为指定类型的变量,并且为它们分配存储空间。


A structure declaration that is not followed by a list of variables reserves no storage; it merely describes a template or shape of a structure. If the declaration is tagged, however, the tag can be used later in definitions of instances of the structure. For example, given the declaration of point above,

如果结构声明的后面不带变量表,则不需要为它分配存储空间,它仅仅描述了一个结构的模板或轮廓。但是,如果结构声明中带有标记,那么在以后定义结构实例时便可以使用该标记定义。例如,对于上面给出的结构声明point,语句


struct point pt;


defines a variable pt which is a structure of type struct point. A structure can be initialized by following its definition with a list of initializers, each a constant expression, for the members:

定义了一个struct point 类型的变量pt。结构的初始化可以在定义的后面使用初值表进行。初值表中同每个成员对应的初值必须是常量表达式,例如:


struct point maxpt = { 320, 200 };

An automatic structure may also be initialized by assignment or by calling a function that returns a structure of the right type.

自动结构也可以通过赋值初始化,还可以通过调用返回相应类型结构的函数进行初始化。



A member of a particular structure is referred to in an expression by a construction of the form

在表达式中,可以通过下列形式引用某个特定结构中的成员:


structure-name.member


The structure member operator ``.'' connects the structure name and the member name. To print the coordinates of the point pt, for instance,

其中的结构成员运算符“.”将结构名与成员名连接起来。例如,可用下列语句打印点pt 坐标:


printf("%d,%d", pt.x, pt.y);


or to compute the distance from the origin (0,0) to pt,

或者通过下列代码计算原点(0, 0)到点pt的距离:

double dist, sqrt(double);

dist = sqrt((double)pt.x * pt.x + (double)pt.y * pt.y);


Structures can be nested. One representation of a rectangle is a pair of points that denote the diagonally opposite corners:

结构可以嵌套。我们可以用对角线上的两个点来定义矩形(参见图6-2),相应的结构定义如下:


struct rect {

struct point pt1;

struct point pt2;

};


The rect structure contains two point structures. If we declare screen as

结构rect包含两个point类型的成员。如果按照下列方式声明screen变量:

struct rect screen;

then

screen.pt1.x

refers to the x coordinate of the pt1 member of screen.

引用screen的成员pt1x坐标。


coordinate  n.坐标,一致


原创粉丝点击