Deps Deps - 1 month ago 11
C Question

Unprintable integer pointer returned to GNU COBOL from C library

I am learning COBOL just for the fun of it and now want to call C functions from my GNU COBOL source.

I can call C functions just fine, however I am having a small problem with a function of mine that looks like this: (It just wraps another function with the same arguments, for now)

int getSensors( char * protocol, int protocolLen,
char * model, int modelLen,
int * id, int * dataTypes ) {
return tdSensor(protocol, protocolLen, model, modelLen, id, dataTypes );
}


My problem is that the value that is returned in the id variable is not printable later in COBOL (TSI-ID below). For example the value returned can be 67, and if I print the variable in COBOL I get the ascii character 'C' instead of the expected value 0067.

The COBOL record looks like this:

01 TELLSTICK-SENSOR-ITER.
05 TSI-PROTOCOL PIC X(50).
05 TSI-MODEL PIC X(50).
05 TSI-ID PIC 9(4).
05 TSI-DATATYPES PIC 9(4).
05 TSI-RETURN PIC S9(4).


And my call looks like this:

CALL "getSensors" USING
BY REFERENCE TSI-PROTOCOL BY VALUE 50
BY REFERENCE TSI-MODEL BY VALUE 50
BY REFERENCE TSI-ID
BY REFERENCE TSI-DATATYPES
RETURNING TSI-RETURN.


I'm new to COBOL and my C skills are quite rusty as I usually work in Java. Is there an obvious newbie error in my code here?

Answer

If you return an int you can directly check the RETURN-CODE variable and don't need to use RETURNING clause at all.

If you want to use it: an int maps to retvar USAGE BINARY-LONG.