I'd like to print what's behind an &myVariable. I tried NSLog(&myIntVar); but it won't work.
+8
A:
The argument to NSLog needs to be an NSString, so you want
NSLog(@"%p", &myIntVar);
smorgan
2009-04-29 13:06:12
Better yet, us @"%p". %x formats an integer value as hexadecimal, whereas %p is specifically for pointers, and additionally pads the hexadecimal representation it prints.
2009-04-29 13:53:09
Good point! I've updated the answer accordingly :)
smorgan
2009-04-29 13:58:23
A:
Try:
NSLog(@"%p", &myIntVar);
or
NSLog(@"%lx", (long)&myIntVar);
The first version uses the pointer-specific print format, which assumes that the passed parameter is a pointer, but internally treats it as a long.
The second version takes the address, then casts it to a long integer. This is necessary for portability on 64-bit platforms, because without the "l
" format qualifier it would assume that the supplied value is an integer, typically only 32-bits long.
Alnitak
2009-04-29 13:06:52
I'd recommend against the second version, you don't gain anything. %p already works as expected on 32 or 64 bit platforms.
2009-04-29 13:57:49
Apple specifically mention the latter method themselves: http://developer.apple.com/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html#//apple_ref/doc/uid/TP40004265-SW1
Alnitak
2009-04-29 15:06:09