POINTER TO STRUCTURE指向结构的指针
- 格式:docx
- 大小:14.12 KB
- 文档页数:3
POINTER TO STRUCTURE
When a structure is defined, part of its definition is the `type' of the structure. In the example under STRUCTURE in this section, the structure type is my_struct. There may be many variables that are defined as being of type my_struct, for example var1, var2 may be defined as being of type my_struct. It is often useful to have a variable that can point to different structure variables (of the same type) at different times. This is handled by defining a variable of type Pointer To Structure.
The pointer to structure variable is defined by using the `@' symbol. The pointer to structure variable is assigned to a structure variable by using the `&' symbol.
EXAMPLE:
In the following example, a structure of type my_struct is defined, with two members.
Two variables var1 and var2 are defined which are structures of type my_struct.
A variable var_struc is defined which is a pointer to structure variable which points to structures of type my_struc. The members of var1 and var2 have values assigned to them. Note the use of the member pointer (.) in assignments. Then var_struc is assigned to point to var1 using the & symbol. The values of the members of the structure pointed to by var_struc are written out. Note that the symbol (->) replaces the use of the member pointer (.) when accessing members for pointer to structure variables.
------- definition of structure -----------------
structure my_struct
-- members of structure
ii : integer
rr : real
endstructure
-------- variable definitions --------------------
var
-- variables defined as being of type my_struct
var1,var2 : my_struct
-- variable defined as being a pointer to structures
-- of type my_struct
var_struc : @my_struct
procedure struct_demo()
begin
-- assign values to the variables for their members
-- note the use of the period
var1.ii = 100
var2.ii = 200
var1.rr = 1.1
var2.rr = 2.2
-- set the structure pointer to point to the structure
-- var1 note the use of &
var_struc = &var1
-- write out the values of the members of var_struc
-- note the use of ->
write (var_struc->ii,' ',var_struc->rr,cr)
-- set the structure pointer to point to the structure
-- var2 note the use of &
var_struc = &var2
-- write out the values of the members of var_struc
-- note the use of ->
write (var_struc->ii,' ',var_struc->rr,cr)
end
指向结构
当一个结构的定义,其定义的一部分是该结构的`型'。在本节下结构的例子中,结构类型为MY_STRUCT。有可能是,被定义的类型是MY_STRUCT,例如var1的许多变量,VAR2可以被定义为类型MY_STRUCT的。它是非常有用的是具有可在不同的时间点,以不同的结构变量(同一类型的)的一个变量。这是通过定义类型的指针变量来构造处理。
以结构体变量的指针是通过使用'@'符号定义。以结构体变量的指针是通过使用'&'符号分配给一个结构体变量。
示例:
在下面的例子中,类型MY_STRUCT的结构定义,有两个成员。
两个变量var1和定义它的类型是MY_STRUCT的结构。
变量var_struc定义它是一个指向结构体变量,它指向型my_struc的结构。var1和成员都分配给他们的价值观。注意:在分配使用的成员指针()的。然后var_struc被分配到使用点的&符号VAR1。结构的成员的值指向var_struc都写了出来。需要注意的是符号- ()(>)取代了使用该成员指针访问成员指针,结构体变量的时候。
-------结构的定义-----------------
结构MY_STRUCT
- 结构的成员
二:整数
RR:实
endstructure
--------变量定义--------------------
变种
- 定义为类型MY_STRUCT变量
VAR1,VAR2:MY_STRUCT
- 变量定义为一个指向结构