Be warned that in Delphi, a dynamic array is not exactly the same as a static array.
A static array is defined as such:
type
TIntegerArray10 = array[0..10] of integer;
PIntegerArray10 = ^TIntegerArray10;
And a dynamic array is defined as:
type
TIntegerDynamicArray = array of integer;
But the dynamic array memory is allocated on the heap by Delphi, using the SetLength() function. You can access to the highest boundary with Length() or High() functions.
The dynamic array can then be mapped back to a static array, via the pointer() function:
var Dyn: TIntegerDynamicArray;
i: integer;
begin
SetLength(Dyn,11); // will create Dyn[0] to Dyn[10]
for i := 0 to high(Dyn) do begin // same as for i := 0 to length(Dyn)-1 do
Dyn[i] := i;
assert(PIntegerArray(pointer(Dyn))^[i]=i);
end;
end;
The char * (pointers of char) can be handled as array of char in Delphi, but it's not the most easy way of doing it, because you'll have to get and free the memory at hand. You can use the AnsiString type, then cast it as a pointer: it will return a PAnsiChar.
So for an array of AnsiString, each array item (accessible by []) will be a PAnsiChar, and the pointer to the whole array of AnsiString will be a PPAnsiChar, i.e. a char **
var Dyn: array of AnsiString;
begin
SetLength(Dyn,19);
for i := 0 to 19 do
Dyn[i] := IntToStr(i);
// now pointer(Dyn) can be mapped to PPAnsiChar or char **
end;
All this is useful to create some content from Delphi, then use it in your C code.
For the contrary, i.e. using C generated char ** into Delphi, you must use pointers to static arrays, not pointer to dynamic arrays. As far as I remember, char ** structures usually end with a NULL to mark the end of array.
Here is what I would recommend to use, to convert a C char ** array into a true Delphi dynamic array (to be adapted to your purpose):
type
TAnsiStringDynArray = array of AnsiString;
TPAnsiCharArray = array[0..maxInt div 4-1] of PAnsiChar;
PPAnsiCharArray = ^TPAnsiCharArray;
procedure ConvertCToDelphi(inArray: PPAnsiCharArray; var outDynArray: TAnsiStringDynArray);
var i, n: integer;
begin
n := 0;
if inArray<>nil then
while inArray[n]<>nil do
inc(n);
SetLength(outDynArray,n);
for i := 0 to n-1 do
outDynArray[i] := AnsiString(InArray[n]); // explicit typing to make Delphi 2009 happy
end;
You can use PPAnsiChar, if you just want to enumerate a char ** array:
procedure Test(FromC: PPAnsiChar);
begin
if FromC<>nil then
while FromC^<>nil do begin
writeln('Item value is ',FromC^); // Delphi will map FromC^ PAnsiChar into a string
inc(FromC); // this will go to the next PAnsiChar in the array
end;
end;