[fpc-pascal] Array as result in function.
Sven Barth
pascaldragon at googlemail.com
Sat Jan 21 12:29:01 CET 2017
On 20.01.2017 23:36, fredvs wrote:
> Re-hello.
>
> Ok, thanks Silvio, I will take this one from your advice, it works like
> charm:
>
> type
> TOggOpusFile = THandle;
> TDArFloat = array of cfloat;
> PDArFloat = ^TDArFloat;
>
> op_read: function(OpusFile: TOggOpusFile; pcm : PDArFloat; SampleCount:
> Integer; li: pointer): Integer;
> op_read_float: function(OpusFile: TOggOpusFile; pcm : PDArFloat;
> SampleCount: Integer; li: pointer): Integer;
Gah! No!
If a C function has a parameter of e.g. "float*" and it's an array you
should use the same type in FPC, in that case "pcfloat". *DO NOT* use a
Pascal dynamic array or (even worse) a pointer to a Pascal dynamic array.
You can of course use a Pascal array when calling it like this:
op_read_float(f, @arr[0], 42, whatever);
If the C code however is the one allocating the array (in which case the
type is usually a pointer to a pointer, like "float**" or "^pcfloat" in
Pascal) then you *must not* use a Pascal array, but instead simply pass
in a "pcfloat" variable like this:
some_func(@mypcfloat);
(though in that case to avoid confusion you *might* declare the
parameter as "var arg: pcfloat" or "out arg: pcfloat" which would be the
equivalent and then using "somefunc(mypcfloat) would work as well")
You can then access the C array like you'd do with a Pascal array with
"[]", like "mypcfloat[42]". Of course the C function should also tell
you the resulting length somehow ;)
I hope this clears up a few points for mixing C and Pascal arrays.
Regards,
Sven
More information about the fpc-pascal
mailing list