簡體   English   中英

如何在Delphi中表示一個以null結尾的字符串數組?

[英]How do I represent a null terminated string array in Delphi?

如何用Delphi表示此C代碼?

static char *mylist[] = {"aaa", "bbb", "ccc", NULL};

我可以從創建數組

keywords : array[0..3] of string;
keywords[0] := 'aaa';
keywords[1] := 'bbb';
keywords[2] := 'ccc';
//Compiler error -> E2010 Incompatible types: 'string' and 'Pointer' 
keywords[3] := nil; 

C / C ++中的char*在Delphi中是PAnsiChar ,例如:

const
  mylist: array[0..3] of PAnsiChar = ('aaa', 'bbb', 'ccc', nil);

要么:

var
  mylist: array[0..3] of PAnsiChar;

mylist[0] := 'aaa';
mylist[1] := 'bbb';
mylist[2] := 'ccc';
mylist[3] := nil;

在Pascal中,數組和字符串是與指針不同的類型,因此您不能分配nil指針。

無論如何,您可能不需要特殊的令牌來終止數組。 這是一個C習語。

如果要遍歷數組,只需執行以下操作:

for word in keywords do
    writeln(word)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM