Originale-mail to me for new edition

 

Working with null-terminated strings

 

Many programming languages, including C and C++, lack a dedicated string data type. These languages, and environments that are built with them, rely on null-terminated strings. A null-terminated string is a zero-based array of characters that ends with NULL (#0); since the array has no length indicator, the first NULL character marks the end of the string. You can use Object Pascal constructions and special routines in the SysUtils unit (see Standard routines and I/O) to handle null-terminated strings when you need to share data with systems that use them.

For example, the following type declarations could be used to store null-terminated strings.

type

  TIdentifier = array[0..15] of Char;

  TFileName = array[0..259] of Char;

  TMemoText = array[0..1023] of WideChar;

With extended syntax enabled ({$X+}), you can assign a string constant to a statically allocated zero-based character array. (Dynamic arrays won’t work for this purpose.) If you initialize an array constant with a string that is shorter than the declared length of the array, the remaining characters are set to #0.

Using pointers, arrays, and string constants

Mixing Pascal strings and null-terminated strings

 

Topic groups

 

See also

About string types

Arrays: Overview

Handling null-terminated strings

 

 

译文

 

空结束串

 

许多变成语言,包括CC++,都缺乏专门的串数据类型。这些语言和开发环境都依赖于空结束(null-terminated)串来建立串。一个空结束串就是一个以NULL#0)结束的零基(zero-based)字符数组;因为该数组没有长度指示,所以第一个NULL字符就标记串尾。当需要向其他使用串的系统共享数据时,可以使用Object Pascal结构和SysUtils单元中的特别例程(见标准例程和I/O)来处理空结束串。

例如,下面声明的类型可以用于存储空结束串:

type

  TIdentifier = array[0..15] of Char;

  TFileName = array[0..259] of Char;

  TMemoText = array[0..1023] of WideChar;

扩展语法有效({$X+})时,可以把一个串常量赋值到一个静态分配的零基字符数组(动态数组不能如此)。如果要初始化一个数组,使其含有一个串,而该串的长度小于数组声明长度时,数组中剩余的字符将被置为#0

使用指针、数组和串常量

混合Pascal串和空结束串

 

主题组

 

相关主题

关于串类型

数组:概述

处理空结束串