Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Be warned that in Delphi, a dynamic array is not exactly the same as a static array.</p> <p>A static array is defined as such:</p> <pre><code>type TIntegerArray10 = array[0..10] of integer; PIntegerArray10 = ^TIntegerArray10; </code></pre> <p>And a dynamic array is defined as:</p> <pre><code>type TIntegerDynamicArray = array of integer; </code></pre> <p>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.</p> <p>The dynamic array can then be mapped back to a static array, via the pointer() function:</p> <pre><code>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; </code></pre> <p>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.</p> <p>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 **</p> <pre><code>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; </code></pre> <p>All this is useful to create some content from Delphi, then use it in your C code.</p> <p>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.</p> <p>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):</p> <pre><code>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&lt;&gt;nil then while inArray[n]&lt;&gt;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; </code></pre> <p>You can use PPAnsiChar, if you just want to enumerate a char ** array:</p> <pre><code>procedure Test(FromC: PPAnsiChar); begin if FromC&lt;&gt;nil then while FromC^&lt;&gt;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; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload