Originale-mail to me for new edition

 

Pointer operators

 

The relational operators <, >, <=, and >= can take operands of type PChar (see Relational operators). The following operators also take pointers as operands. For more information about pointers, see Pointers and pointer types.

 

Operator

Operation

Operand types

Result type

Example

+

pointer addition

character pointer, integer

character pointer

P + I

-

pointer subtraction

character pointer, integer

character pointer, integer

P - Q

^

pointer dereference

pointer

base type of pointer

P^

=

equality

pointer

Boolean

P = Q

<>

inequality

pointer

Boolean

P <> Q

 

The ^ operator dereferences a pointer. Its operand can be a pointer of any type except the generic Pointer, which must be typecast before dereferencing.

P = Q is True just in case P and Q point to the same address; otherwise, P <> Q is True.

You can use the + and - operators to increment and decrement the offset of a character pointer. You can also use - to calculate the difference between the offsets of two character pointers. The following rules apply.

·  If I is an integer and P is a character pointer, then P + I adds I to the address given by P; that is, it returns a pointer to the address I characters after P. (The expression I + P is equivalent to P + I.) P - I subtracts I from the address given by P; that is, it returns a pointer to the address I characters before P.

·  If P and Q are both character pointers, then P - Q computes the difference between the address given by P (the higher address) and the address given by Q (the lower address); that is, it returns an integer denoting the number of characters between P and Q. P + Q is not defined.

 

Topic groups

 

See also

About operators

Operator precedence rules

 

 

译文

 

指针运算符

 

关系运算符<><=、和 >= 能以PChar类型作为操作数进行运算(见关系运算符)。下面的运算符还能以指针作为操作数进行运算。关于指针的更多信息,见指针和指针类型

 

运算符

操作

操作数类型

结果类型

范例

+

指针加

字符指针,整数

字符指针

P + I

-

指针减

字符指针,整数

字符指针,整数

P - Q

^

指针解除参照

指针

指针的基类型

P^

=

相等

指针

Boolean

P = Q

<>

不等

指针

Boolean

P <> Q

 

运算符 ^ 用于解除指针参照。该运算符的操作数可以是任何类型的指针,但不能是一般的Pointer类型的指针(如果需要,那么在解除指针参照前必需进行类型转换)。

表达式P = Q的值为True当且仅当PQ指向相同的地址;否则表达式P <> Q的值为True

可以使用加号(+)和减号(-)运算符对字符指针的偏移量递增和递减;还可以使用减号(-)计算两个字符指针偏移量的差(此时运算结果是整数类型)。此外,下列规则适用于指针运算:

·  如果 I 是整数并且 P 是字符指针,那么 P + I 将对P给出的地址增加 I ;也就是说,表达式将返回字符指针 P 之后 I 个字符的地址。(表达式 I + P 等价于表达式 P + I。)表达式 P - I 将对P给出的地址减少 I ;也就是说,表达式将返回字符指针 P 之前 I 个字符的地址。

·  如果PQ都是字符指针,那么P - Q将计算P给出的地址(较高的地址)与Q给出的地址(较低的地址)之间的差;也就是说,表达式返回的整数值表示字符指针PQ之间字符的个数。表达式P + QObject Pascal中没有定义。

 

主题组

 

相关主题

运算符

运算符优先规则

 

 

编者注

·当指针P1P2均为nil(空指针)时,它们均不指向任何地址,这时表达式P1 = P2的结果也是True。如果P1 = nil并且P2 <> nil,那么P1 <> P2

·IN为整数且P为字符指针时,I + P 等价于 P + I,表达式I + P + NI + N + PP + I + NP - I - NP + I - N等都是合法的,而表达式I - P将不被编译器认可,因而是非法的。

·对于字符指针PQ,如果P - Q返回的整数值大于零,那么表示PQ之后;如果返回零,那么表示P = Q;如果小于零,那么表示PQ之前。