%p and different pointer representations
Daniel F. Fisher
dff at Morgan.COM
Fri Feb 24 08:48:25 AEST 1989
In article <9382 at bloom-beacon.MIT.EDU> scs at adam.pika.mit.edu
(Steve Summit) writes:
>
>No, I just use %p.
>
Consider a hosted C implementation in which different pointers
have different representations, i.e. (char *) is different from
(int *) is different from (int (*)()), etc. My question is
To what type should a pointer argument be cast when passing it to
fprintf() for printing using the %p conversion specifier?
In view of the following two examples, I don't see that there is
a single correct answer that will work in all architectures. And
since %p is part of dANS C, I assume that the answer must be the
same for every pointer in every conforming hosted implementation.
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. One the other hand,
since the type cast used for both function pointers and data
pointers should be the same, I must cast all data pointers to
something that is as wide as a function pointer.
Second Example: In an implementation on an architecture that is
NOT byte addressable, int pointers and function pointers will be
word addresses and character pointers will be wider to contain
both a word address and a byte offset. So if I wish to print a
character pointer, I cannot cast it to an int pointer or function
pointer without losing information. One the other hand, since the
type cast used for all pointers must be the same, I must cast int
pointers and function pointers to something as wide as a character
pointer.
So in the first example, I want to cast everything to be as wide
as a function pointer and in the second, I want to cast everything
to be as wide as a character pointer (which is wider than a function
pointer). What's a programmer to do? Now if someone wants to tell
me that I should cast to (void *) because it is guaranteed to be at
least as wide as any pointer type in the implementation, then I will
say, "Thank you very much", "Wonderful", "Hooray for void star" and
"Gee, I didn't know that".
Well, what gives?
--
Daniel F. Fisher
dff at morgan.com
More information about the Comp.lang.c
mailing list