[comp.lang.c] portably printing pointers

chris@mimsy.umd.edu (Chris Torek) (08/13/90)

In article <171@srchtec.UUCP> johnb@srchtec.UUCP (John Baldwin) writes:
>[%p] solves a lot of problems for portability, and I for one am very glad
>I was wrong (I've been grumbling all this time about not being able to
>portably print pointers).

Although ANSI says that `%p' prints a pointer (of type `void *') and thus
this is in a sense `portable', it also goes on to say `in an implementation-
defined manner', so in another sense this is not anywhere near portable.
One possible manner for printing a pointer would be to print a mailing
address for one house/apartment/whatever somewhere in the world, with
a different (and unique) address for each possible pointer value.

	printf("%p %p %p", (void *)ptr1, (void *)ptr2, (void *)ptr3);

might produce

	9191 Farrel Avenue
	Columbus, OH

	12500 Lakeshore Drive
	Chicago, IL

	17 Ashburn Pl
	Avenue, MD

(Yes, there is a town in MD called `Avenue'.  I made up the other parts
though.)
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@cs.umd.edu	Path:	uunet!mimsy!chris
	(New campus phone system, active sometime soon: +1 301 405 2750)

colin@array.UUCP (Colin Plumb) (08/15/90)

In article <25995@mimsy.umd.edu> chris@mimsy.umd.edu (Chris Torek) writes:
-> Although ANSI says that `%p' prints a pointer (of type `void *') and thus
-> this is in a sense `portable', it also goes on to say `in an implementation-
-> defined manner', so in another sense this is not anywhere near portable.
-> One possible manner for printing a pointer would be to print a mailing
-> address for one house/apartment/whatever somewhere in the world, with
-> a different (and unique) address for each possible pointer value.
->
->	printf("%p %p %p", (void *)ptr1, (void *)ptr2, (void *)ptr3);
->
-> might produce
->
->	9191 Farrel Avenue
->	Columbus, OH
->
->	12500 Lakeshore Drive
->	Chicago, IL
->
->	17 Ashburn Pl
->	Avenue, MD

I'd like to suggest to implementors, though, that %p not produce
strings containing characters for which isprint() is false.
Doing so makes things like "% 10p" very hard to use.  Indeed,
isgraph() is probably advisable, to allow the bounds of the pointer's
printed form to be delimited by whitespace.

(I would go so far as to suggest that ANSI would have codified it,
had anyone raised the possibility at the appropriate time.)
-- 
	-Colin