%p and different pointer representations
Chris Calabrese[mav]
cjc at ulysses.homer.nj.att.com
Tue Feb 28 06:40:38 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.
[Other examples and arguments deleted]
#define Flame
Yes, but the whole point of %p and void is to make such things
_more_ portable, where as this example _only_ works on 80*86
architectures, and relies on keywords that aren't even part of C,
but are a non-conforming polution of keyword name space invented
for IBM PC compilers.
#undef Flame
The point is, %ld works on 99% of the machines out there now,
why should I use %p if it makes things even less portable?
The representation of the pointer is non-portable,
but accessing it should not be. I can see that you
don't really need to pass around function pointers as void*,
but there should be a way of printing their values!
However, please don't give me
%h - prints a pointer to a function
(h is the first free letter after f, which is taken)
--
Name: Christopher J. Calabrese
Brain loaned to: AT&T Bell Laboratories, Murray Hill, NJ
att!ulysses!cjc cjc at ulysses.att.com
Obligatory Quote: ``Now, where DID I put that bagel?''
More information about the Comp.lang.c
mailing list