<<Navigation: 执行失败 ['AllContext' object has no attribute 'values'] (see also the log)>>

Chapter 6 - Structures 结构

【czk注:本章所用的结构一词,即平时所说的结构体。不要与第4章讲述的程序结构或块结构所混淆。】

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标准中,自动结构和数组现在也可以进行初始化。

TCPL/6_Structures (2008-05-22 11:44:13由czk编辑)

ch3n2k.com | Copyright (c) 2004-2020 czk.