简体   繁体   English

如何在Delphi中表示一个以null结尾的字符串数组?

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

How can I represent this C code in Delphi? 如何用Delphi表示此C代码?

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

I can create my array as of 我可以从创建数组

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; 

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

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

Or: 要么:

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

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

In Pascal, arrays and strings are a distinct type from pointers, so you can't assign the nil pointer. 在Pascal中,数组和字符串是与指针不同的类型,因此您不能分配nil指针。

You probably don't need a special token to terminate your array anyway. 无论如何,您可能不需要特殊的令牌来终止数组。 This is a C idiom. 这是一个C习语。

If you want to loop through your array, simply do this : 如果要遍历数组,只需执行以下操作:

for word in keywords do
    writeln(word)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM