KnowledgeBase Archive

An Archive of Early Microsoft KnowledgeBase Articles

View on GitHub

Q23977: Creating Pointers to Specific Addresses in Memory

Article: Q23977
Product(s): See article
Version(s): 3.00 4.00 5.00 5.10 6.00 6.00a
Operating System(s): MS-DOS
Keyword(s): ENDUSER | s_quickc | mspl13_c
Last Modified: 6-FEB-1991

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.

THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Copyright Microsoft Corporation 1986-2002.