Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!csd4.milw.wisc.edu!leah!itsgw!steinmetz!uunet!mstan!dff From: dff@Morgan.COM (Daniel F. Fisher) Newsgroups: comp.lang.c Subject: %p and different pointer representations Summary: In fprintf, what kind of pointer is used for %p Message-ID: <234@mstan.Morgan.COM> Date: 23 Feb 89 22:48:25 GMT References: <9382@bloom-beacon.MIT.EDU> Reply-To: dff@Morgan.COM (Daniel F. Fisher) Organization: Morgan Stanley and Co., NY, NY Lines: 51 In article <9382@bloom-beacon.MIT.EDU> scs@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@morgan.com