Originale-mail to me for new edition

 

Mixing Pascal strings and null-terminated strings

 

You can mix long strings (AnsiString values) and null-terminated strings (PChar values) in expressions and assignments, and you can pass PChar values to functions or procedures that take long-string parameters. The assignment S := P, where S is a string variable and P is a PChar expression, copies a null-terminated string into a long string.

In a binary operation, if one operand is a long string and the other a PChar, the PChar operand is converted to a long string.

You can cast a PChar value as a long string. This is useful when you want to perform a string operation on two PChar values. For example,

S := string(P1) + string(P2);

You can also cast a long string as a null-terminated string. The following rules apply.

·  If S is a long-string expression, PChar(S) casts S as a null-terminated string; it returns a pointer to the first character in S.

On Windows: For example, if Str1 and Str2 are long strings, you could call the Win32 API MessageBox function like this:

MessageBox(0, PChar(Str1), PChar(Str2), MB_OK);

(The declaration of MessageBox is in the Windows interface unit.)

On Linux: For example, if Str is a long string, you could call the opendir system function like this:

opendir(PChar(Str));

(The declaration of opendir is in the Libc interface unit.)

·  You can also use Pointer(S) to cast a long string to an untyped pointer. But if S is empty, the typecast returns nil.

·  When you cast a long-string variable to a pointer, the pointer remains valid until the variable is assigned a new value or goes out of scope. If you cast any other long-string expression to a pointer, the pointer is valid only within the statement where the typecast is performed.

·  When you cast a long-string expression to a pointer, the pointer should usually be considered read-only. You can safely use the pointer to modify the long string only when all of the following conditions are satisfied.

·  The expression cast is a long-string variable.

·  The string is not empty.

·  The string is unique, that is, has a reference count of one. To guarantee that the string is unique, call the SetLength, SetString, or UniqueString procedure.

·  The string has not been modified since the typecast was made.

·  The characters modified are all within the string. Be careful not to use an out-of-range index on the pointer.

The same rules apply when mixing WideString values with PWideChar values.

 

Topic groups

 

See also

Using pointers, arrays, and string constants

Working with null-terminated strings

 

 

译文

 

混合Pascal串和空结束串

 

在赋值语句和表达式中可以混合使用长串(AnsiString值)和空结束串(PChar值),也可以传递PChar值到那些接受长串参数的函数或过程。对于长串变量SPChar表达式P,赋值语句S := P将赋值空结束串到长串中。在二进制运算中,如果一个操作数是长串而另一个是PChar,那么PChar操作数将被变换为长串,然后在运算。

可以将PChar值转换为长串,当对两个PChar值执行串运算时是很有用的。例如,

S := string(P1) + string(P2);

 

也可以将长串转换为空结束串,规则如下:

·  如果S是一个长串表达式,那么PChar(S)S转换为一个空结束串;它返回串S中第一个字符的指针。

对于Windows:例如,如果Str1Str2都是长串,那么可以如下调用Win32 API函数MessageBox

MessageBox(0, PChar(Str1), PChar(Str2), MB_OK);

MessageBox声明于Windows接口单元)

对于Linux:例如,如果Str是一个长串,那么可以如下调用系统函数opendir

opendir(PChar(Str));

opendir声明于Libc接口单元)

·  还可以用Pointer(S)将长串转换为一个无类型指针。如果S为空,那么类型转换返回空指针nil

·  转换长串变量到指针时,指针一直维持有效状态,直到变量被赋予新值或程序执行点已经离开作用域。如果转换其他任何长串表达式到指针,那么指针仅在类型转换被执行的语句中有效。

·  转换长串表达式到指针时,指针通常被认为是只读的。仅当下列条件全部满足时,可以安全地使用指针修改长串:

·  被转换的表达式是一个长串变量。

·  串非空

·  串是唯一的(即引用计数为1)。要保证串唯一,可以调用SetLengthSetStringUniqueString等过程。

·  自类型转换完成以来串未曾被修改

·  修改过的字符都在串内。注意不要使用超出范围的指针索引

 

相同的规则适用于混合WideString值和PWideChar值。

 

主题组

 

相关主题

使用指针、数组和串常量

空结束串