Passing COBOL items into C functions by reference
C functions frequently receive arguments for reference modification.
The most prevalent example of this is a C-style string modification
where a character array is received via a copy of a pointer to the
original string. Items may be passed from COBOL to C for reference
modification using the BY REFERENCE
phrase inside the CALL
statement.
The following example demonstrates such a situation.
Example:
C function prototype of receiving function:
int receiveString(char inString[256]);
Definitions for the working storage item being passed as an argument and the return value:
01 THE-STRING PIC X(256).
01 RETVAL PIC S9(9) BINARY.
The CALL statement in the COBOL program:
CALL “receiveString” USING BY REFERENCE THE-STRING RETURNING RETVAL.
Example 2: A C function that receives a pointer to an integer from the calling COBOL program:
C function prototype:
int changeInt(int * fromCOBOL);
Working storage entries in the calling COBOL:
01 THE-INT PIC S9(9) BINARY.
01 RETVAL PIC S9(9) BINARY.
The CALL statement in the COBOL program:
CALL “changeInt” USING BY REFERENCE THE-INT RETURNING RETVAL.