Rudolf Schubert <rudolf@muc.de> writes:
High Gaius,
I'm facing a problem when trying to pass a parameter of typ String256
to C, which I've defined like this:
CONST
len256= 256;
TYPE
String256= ARRAY[0..len256-1] OF CHAR;
In the appended example I've set up three DEFINITION MODULEs
(unix1.def, unix2.def, unix3.def) each with a corresponding
C file (unix1.c, unix2.c, unix3.c). There is only one simple
function in these C files which should print out the length
of the passed string and the string proper.
unix1.def does NOT have 'FOR "C"' whereas unix2.def and unix3.def
do have a 'FOR "C"' in the MODULE definition. In unix1.def and
unix2.def
the PROCEDURE ParamTest has got (my_str: String256) but in unix3.def
I'm using 'varargs' (...).
I'm seeing that only unix3 does give the expected results. unix1 and
unix2 do either crash the programm or return wrong results.
I do observe similar behaviour not only for
type 'ARRAY[0..MAXINDEX] OF CHAR'
but for instance also for
type 'ARRAY[0..MAXINDEX] OF CARDINAL'
Now my question is: how would I pass parameters of this type to
C functions? I would suspect that this should not only work for
'varargs' but also in the other cases? When using open ARRAYs in
the PROCEDURE things are also working fine. It's just when using
'non open' ARRAYs that the problem occurs.
BR
Hi Rudolf and Benjamin,
Benjamin Kowarsch <trijezdci@gmail.com> write:
but I would like to recommend a different approach which is to use
open array parameters.
indeed I agree - it makes for easier, cleaner and safer code (and it
also allows "hello world" to be passed by address without requiring a
temporary variable). I guess sometimes this is not an option though.
Thanks for the code Rudolf - all very useful - I've added:
unix4: an example of unbounded array.
unix5: pass by value (for string256).
unix6: pass by address (for string256). Interesting to note that
"Hello world" cannot be
passed in this example.
to the original tarball for reference,
hope this helps? Probably this should be written up in the
documentation
regards,
Gaius