%p and different pointer representations

Walter Bright bright at Data-IO.COM
Tue Feb 28 04:22:40 AEST 1989


In article <9453 at bloom-beacon.MIT.EDU> scs at adam.pika.mit.edu (Steve Summit) writes:
>Under Microsoft's PC C compiler (I don't know about others), %p
>expects a 32-bit ("far") pointer, in all memory models.  Therefore,
>the pointer passed must, in general, be cast to "void far *".
>Although this, I suppose, violates the standard, it's probably
>the right solution for that architecture, since not forcing a far
>pointer admits the possibility of losing information.
>I do think Microsoft's compromise was a good one,
>under the circumstances.  (A better one might have been to make
>%p strictly standard-conforming albeit potentially useless, and
>use %P as a nonstandard extension to force 32 bit pointers.)

Under Zortech's printf(), %p expects a pointer of the default data pointer
size. %lp always expects a far pointer.
Thus, to print each type of pointer in each memory model:
	void *		func *		void near *	void far *
S	%p		%p		%x		%lp
M	%p		%lp		%x		%lp
C	%p		%x		%x		%lp
L	%p		%p		%x		%lp

Things to note:
1. Using default data pointers, %p always works.
2. Using far pointers, %lp always works, it's easy to remember because
   of the similarity with %lx.
3. Using near pointers, %x always works. It seemed redundant to add a
   special flag for %p for near pointers, since %x did the job nicely.
4. Be careful using function pointers! A compromise is to cast them to
   (void far *), and use %lp on them.

P.S. I wrote it.



More information about the Comp.lang.c mailing list