Tuesday 8 November 2011

How do you print an address? in C programming

How do you print an address?

The safest way is to use printf() (or fprintf() or sprintf()) with the %P specification. That prints a void
pointer (void*). Different compilers might print a pointer with different formats. Your compiler will pick
a format that’s right for your environment
If you have some other kind of pointer (not a void*) and you want to be very safe, cast the pointer to a void*:
printf( “%P\n”, (void*) buffer );
There’s no guarantee any integer type is big enough to store a pointer. With most compilers, an unsigned
long is big enough. The second safest way to print an address (the value of a pointer) is to cast it to an unsigned
long, then print that.

Cross Reference:

None.

No comments:

Post a Comment