I'm trying to understand how to (or if it is possible to) pass a long
to a C function by value. Here's my simple test C function:
#include <stdio.h>
long addone (long x) {
printf("c got %li\n", x);
fflush(stdout);
return 1+x;
}
And here's my cobol code:
identification division.
program-id. ccalltest.
data division.
working-storage section.
01 my-val usage binary-long.
01 my-rc usage binary-long.
procedure division.
move 10 to my-val.
call 'addone' using
by content my-val
giving my-rc.
display my-val.
display my-rc.
stop run.
I compile with
cobc -x -free cobolcode.cob ccode.c -lc
What actually happens, though, is C receives and prints a suspiciously large number (which may actually be a pointer, I'm not sure), instead of the value that I expect, 10. Am I doing something wrong here or is this use case not supported?
Here's an example of the program output (the non-10 numbers change from run to run):
c got 140725928122192
+0000000010
+1324668753
I'm using GnuCOBOL 3.1.2.0 and GCC 14.2.1.
Solution: by value
As pointed out by the commenters, the solution is to use by value
, not by content
. This is actually explained in the GnuCOBOL programmer's guide, section 7.8.5 (the CALL statement). The programmer's guide can be found at this link: https://gnucobol.sourceforge.io/guides.html