I couldn’t find a way to compute something like C offsetof in COBOL code. What I could manage to figure out how to do is compare addresses of a runtime instantiation of the structure, effectively doing this indirectly. Here’s the ugly mess that I cooked up:
I couldn’t figure out the right syntax to do a single compute statement that was just the difference of addresses, as I got numeric/pointer compare errors from the compiler, no matter what I tried. I think that ‘USAGE IS POINTER’ may be required on my variables, but that would still require a temporary. I’m probably either doing this the hard way, or there is no easy way in COBOL.
This program was run with the following simple JCL
//A EXEC PGM=TESTPROG
//SYSOUT DD SYSOUT=*
//STEPLIB DD DSN=COBRC.NATIVE.TESTPROG,
and produced the following SYSOUT
address of TESTPROG-STRUCT = 0016800264
offsetof(ARRAY-NAME,RUECK-BKL) = 0000000002
offsetof(ARRAY-NAME,RUECK-BS) = 0000000004
offsetof(ARRAY-NAME,RUECK-SF) = 0000000007
sizeof(ARRAY-NAME(1)) = 0000000019
Looking at that output, we can conclude the following:
- PIC S9(3) COMP-3 is effectively horrible eye-burning syntax for a “short”
- There is no alignment padding between fields, nor end of array-member padding to force natural alignment of the next array element, should the structure start have been aligned.
I knew the latter, but wasn’t sure what size the first field was, and thought that trying to figure it out with COBOL code would be a good learning exercise.