INF: Creating Pointers to Specific Addresses in Memory

ID Number: Q23977

3.00 4.00 5.00 5.10 6.00 6.00a 6.00ax 7.00

MS-DOS

Summary:

In Microsoft C versions 5.0, 5.1, 6.0, 6.0a, 6.0ax, and C/C++ version

7.0, accessing specific addresses in memory in Microsoft C can be easily

accomplished by using the FP_SEG and FP_OFF macros or by creating a far

pointer directly from a long.

The FP_SEG and FP_OFF macros are defined in DOS.H, and can be used to

set or get the segment and offset of a far pointer.

The following example shows how FP_SEG and FP_OFF could be used to

create a pointer for directly accessing video memory:

char far *p;

FP_SEG(p) = 0xb800; /* point to segment of video memory */

FP_OFF(p) = 0; /* set offset to first screen position */

See the C run-time library documentation or online Help supplied with

your compiler for more information on FP_SEG and FP_OFF.

You can also access a specific address in memory by casting a long to

a far pointer, as in the following example:

p = (char far *) 0xb8000000L; /* video memory again */

When converting a pointer to or from a long, the upper 16 bits hold

the segment, and the lower 16 bits hold the offset.

Additional reference words: 5.00 5.10 6.00 6.00a 6.00ax 7.00