%p and different pointer representations

thomas.j.roberts tjr at cbnewsc.ATT.COM
Tue Feb 28 01:18:18 AEST 1989


 In article <234 at mstan.Morgan.COM> dff at Morgan.COM (Daniel F. Fisher) writes:
>To what type should a pointer argument be cast when passing it to
>fprintf() for printing using the %p conversion specifier?
>First Example: In an IBM-PC implementation with small-data,
>large-code, the data pointers (int *), (char *), etc. are all 16
>bits., but function pointers (int (*)()), etc. are all 32 bits.
>So if I wish to print a function pointer, I cannot cast it to a
>data pointer without losing information.

In TURBO C, you can do:
	int *p;
	printf("%Fp",(void far *)p);
in ANY MEMORY MODEL, for any size of data pointer p, and get printed
"XXXX:YYYY", where XXXX is the segment, and YYYY is the offset for pointer p.
If p is a small data pointer, DS will be supplied by the compiler.
Note that using the "near" keyword is dangerous in a large-data memory model,
because DS != SS, and DS is supplied when converting near pointers to far
pointers. I believe that the keywords "_ds", "_es", and "_ss" are intended
to permit proper use of small pointers in a large-pointer memory model.

For a function pointer, do:
	typedef int far (*FAR_FUN_PTR)(); /* NOT TESTED */
	printf("%Fp",(FAR_FUN_PTR)p);
Again, the "near" keyword is dangerous - it is not clear whether it can 
sensibly be used with function pointers in a large-code memory model.
In a large-code model, simply do:
	int (*fun_ptr)();
	printf("%Fp",fun_ptr);

Tom Roberts
att!ihnet!tjr



More information about the Comp.lang.c mailing list