Question

How can I represent this C code in Delphi?

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; 
Was it helpful?

Solution

char* in C/C++ is PAnsiChar in Delphi, eg:

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;

OTHER TIPS

In Pascal, arrays and strings are a distinct type from pointers, so you can't assign the nil pointer.

You probably don't need a special token to terminate your array anyway. This is a C idiom.

If you want to loop through your array, simply do this :

for word in keywords do
    writeln(word)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top