16.3.4 Structures and Unions

H2INC translates C structures and unions into their MASM equivalents. H2INC modifies the C structure or union definition to account for differences from MASM structure and union definitions. This list describes these modifications.

C allows a structure or union variable to have the same name as the type name, but MASM does not. The H2INC /Zu option prevents the structure name from matching a variable or instance by prefixing every MASM structure name with @tag_.

If a C structure or union definition does not have a name, H2INC supplies one for the MASM conversion. These generated structure names take the form @tag_n, where n is an integer that starts at zero and is incremented for each structure name H2INC generates.

If the /Zn option is specified, H2INC inserts the given string between the underscore and the number in the generated structure names. This eliminates name conflicts with other H2INC-generated include files.

H2INC adds the alignment value to the converted structure definition.

The following examples show how these rules are applied when converting structures. (Union conversions are not shown; they are handled identically.) These examples assume that the C header file defines an alignment value of 2. (See Section 5.2.1, “Declaring Structure and Union Types,” for information on alignment values.)

The following named C structure definition

struct file_info

{

unsigned char file_addr;

unsigned int file_size;

};

is converted to the following MASM form. Except for explicitly specifying the alignment value, the conversion is direct:

file_info STRUCT 2t

file_addr BYTE ?

file_size WORD ?

file_info ENDS

If the same C structure definition is converted using the /Zu option, the @tag_ prefix is added to the structure's name so that the name does not duplicate the name of a structure component:

@tag_file_info STRUCT 2t

file_addr BYTE ?

file_size WORD ?

@tag_file_info ENDS

If the original C structure definition is modified to be an unnamed-type declaration of a specific instance (myfile)

struct

{

unsigned char file_addr;

unsigned int file_size;

} myfile ;

its MASM conversion looks like the following example. (The specific integer added to the @tag_ prefix is determined by the sequence in which H2INC creates tag names.)

@tag_7 STRUCT 2t

file_addr BYTE ?

file_size WORD ?

@tag_7 ENDS

EXTERNDEF C myfile:@tag_7

Nested structures may have as many levels as desired; they are not limited to one level. Nested structures are “unnested” (expanded) in the correct hierarchical sequence, as shown with the C structure and H2INC-generated code in this example.

/* C code: */

struct phone

{

int areacode;

long number;

};

struct person

{

char name[30];

char sex;

int age;

int weight;

struct phone;

} Jim;

; H2INC generated code:

phone STRUCT 2t

areacode SWORD ?

number SDWORD ?

phone ENDS

person STRUCT 2t

name SBYTE 30t DUP (?)

sex SBYTE ?

age SWORD ?

weight SWORD ?

STRUCT

areacode SWORD ?

number SDWORD ?

ENDS

person ENDS

EXTERNDEF C Jim:person

See Section 5.2 for information on MASM structures and unions.