Declaring Near, Far, Huge, and Based Variables

The __near, __far, __huge, and __based keywords can modify either objects or pointers to objects. When using them to declare variables, keep these rules in mind:

The keyword always modifies the object or pointer immediately to its right. In complex declarations, think of the __far keyword and the item to its right as being a single unit.

For example, in the case of the declaration

char __far * __near *p;

p is a near pointer to a far pointer to char, which resides in the default data segment for the memory model being used.

By contrast, the declaration

char __far * __near p;

is a far pointer to char that will always be stored in DGROUP, regardless of the memory model being used.

If the item immediately to the right of the keyword is an identifier, the keyword determines whether the item will be allocated in the default data segment (__near) or a separate data segment (__far, __huge, or __based). For example,

char __far a;

allocates a as an item of type char with a __far address.

If the item immediately to the right of the keyword is a pointer, the keyword determines whether the pointer will hold a near address (16 bits), a based address (16 bits), a far address (32 bits), or a huge address (also 32 bits). For example,

char __huge *p;

allocates p as a huge pointer (32 bits) to an item of type char. Any arithmetic performed on the huge pointer p will affect all 32 bits. That is, the instruction p++ increments the pointer as a 32-bit entity.