Matching Complex Data Items

Matching complex data items such as C structure (struct) items or arrays is more involved. The challenge 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  DATA-1  PIC X(5).
    03  INT-1   SIGNED-INT.

Assuming that each item is allocated 4 bytes, this would seem to match the following C structure:

struct {
    char  data_1[5];
    int   int_1;
} group_1;

However, it most likely won't match up with the default structure packing due to alignment. 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 approach requires 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. For example, you could set up two different directories on the development machine, one for "-Dw32" objects, and one for "-Dw64" objects. This approach would provide you with COBOL objects for all currently supported machines.
Note:

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.