tags:

views:

93

answers:

2

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
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.
Good point! I've updated the answer accordingly :)
smorgan
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
I'd recommend against the second version, you don't gain anything. %p already works as expected on 32 or 64 bit platforms.
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