KnowledgeBase Archive

An Archive of Early Microsoft KnowledgeBase Articles

View on GitHub

Q43173: Using printf with %p in Small or Medium Model

Article: Q43173
Product(s): See article
Version(s): 5.10   | 5.10
Operating System(s): MS-DOS | OS/2
Keyword(s): ENDUSER | | mspl13_c
Last Modified: 17-MAY-1989

When using the %p format in the C Run-Time Library function printf()
in the small or medium memory model, the corresponding argument must
be cast to a far pointer. If the argument is not cast to a far
pointer, the segmented address of the pointer will not be displayed
correctly.

This behavior occurs because printf does not have a formal parameter
list that will automatically cause the type conversion to take place.
In small or medium memory model, a pointer argument will be pushed
onto the stack as a near address if it is not cast to a far pointer,
i.e., only the offset is pushed onto the stack. At run time, printf
sees %p and then assumes both the segment and the offset for the
corresponding argument are pushed onto the stack. This action causes
the function to print an incorrect segment for that argument.
Explicitly casting the argument to a far pointer will force the
segment address to be pushed onto the stack as well.

The following program will point the wrong content of the variable ch:

/* sample program */
#include <stdio. h>

char ch[1] ;
void main (void)
{
printf ("ch = %p\n", ch) ;    /* wrong !! */
}
/* end of sample program */

The following is the correct statement in small and medium model:

    printf ("ch = %p\n", (char far *) ch) ;

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.