


To match simple C external variables or passed data items, just choose the appropriate usage type. For example, to share an integer variable with a C routine, declare it in COBOL as:
77 MY-SHARED-INT SIGNED-INT, EXTERNAL.
In C, this item is then declared as:
int my_shared_int;
Note that you must also name "my_shared_int" in the table of external identifiers, as described in the "direct.c" file.
Here is an example of passing an "int" value in a portable fashion to a C routine:
77 MY-INT SIGNED-INT.
MOVE 123 TO MY-INT.
CALL "C-ROUTINE" USING, BY VALUE, MY-INT.
The ANSI C routine could then read:
void c_routine( int param1 )
{
printf( "This should be `123': %d\n", param1 );
}
Matching complex data items such as C "struct" items or arrays is more difficult. The problem arises in trying to match the FILLER that may follow the COBOL data items. For example, consider the following group item:
01 GROUP-1.
03 INT-1 SIGNED-INT.
03 INT-2 SIGNED-INT.
Assuming that each item was allocated 4 bytes, this would match the following C structure on a 32-bit machine:
struct {
int int_1;
int int_2;
} group_1;
This is straightforward. However, on a 16-bit machine, you would need this C structure:
struct {
int int_1;
int filler_1;
int int_2;
int filler_2;
} group_1;
This structure adjusts for the implied filler that happens when the 4-byte SIGNED-INT items are remapped by the runtime to 2-byte items.
If you must match complex C data types, you can take one of three approaches:
1. You can use fixed-size COMP-5 COBOL data types that match your C structures. You will then have to change your COBOL code and recompile when you move to a different target environment.
2. You can use the variable-size COBOL data types described in this section and adjust your C structures accordingly. This will require a change to your C code when you move to a new environment.
3. You can use the variable-size COBOL data types described in this section and select different target architectures with the "-Dw" compile option. In this scenario, you do not have to change code to go to a new environment--you just have to recompile with a different "-Dw" setting. One possibility would be to setup three different directories on the development machine--one for "-Dw16" objects, one for "-Dw32" objects, and one for "-Dw64" objects. This would provide you with COBOL objects for all currently supported machines.
Note that most C compilers align structure elements according to their own needs. The automatic synchronization, that occurs with variable-size data items, matches the most natural alignment boundaries. But the automatically synchronized data items may not match with the alignment rules used by a particular C compiler. As a result, you may find yourself forced to make some code adjustments for a particular machine.