Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!cwjcc!tut.cis.ohio-state.edu!osu-cis!att!cbnewsc!tjr From: tjr@cbnewsc.ATT.COM (thomas.j.roberts) Newsgroups: comp.lang.c Subject: Re: %p and different pointer representations Message-ID: <173@cbnewsc.ATT.COM> Date: 27 Feb 89 15:18:18 GMT References: <16112@mimsy.UUCP> Organization: AT&T Bell Laboratories Lines: 32 In article <234@mstan.Morgan.COM> dff@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