Skip to main content

This article describes the proper alignment of data within a COBOL group item using the ‘h2cpy’ utility.

Problem:

When creating COBOL applications which use C language routines and data is passed within a struct (structure) the alignment rules for COBOL do not always agree with the rules used when defining the struct in C. When a struct is defined in C, the alignment of data items on proper boundaries occurs automatically. When attempting to duplicate the definition of the struct in COBOL, there is no automatic adjustment for alignment rules. How is it possible to achieve alignment of data within C language structures?

Resolution:

An easy way to achieve the proper alignment of data within a COBOL group item is to use the utility ‘h2cpy’ which is delivered with the Server Express product. The h2cpy utility uses a C language header file (.h) as input and it produces a COBOL copyfile (.cpy). The structures found in the .h file will be translated to COBOL data item definitions with the appropriate padding added to achieve the alignment of data items.

Attached to this document is a small C language header file, the equivalent COBOL copyfile can be produced using the h2cpy utility.

Here is the header file:

> cat demohdr.h

struct DEMOHDR {

char status1;

char status2;

int c_integer;

short c_short;

char c_array[12];

int c_last;

};

>

Here is the equivalent COBOL definition for the DEMOHDR structure.

01 DEMOHDR is typedef.

02 status1 usage char.

02 status2 usage char.

02 filler pic x(2).

02 c-integer usage int.

02 c-short usage short.

02 c-array pic x(12).

02 filler pic x(2).

02 c-last usage int.

Note: The utility generated a number of filler items to force the alignment of integer data items to agree with the alignment enforced when using the C language. In the interest of brevity only a portion of the COBOL copy file is shown. In actual use the utility creates a set of ‘type defintions’ in addition to the definition of the data structure.

Old KB# 14401