Thanks
Thanks

Reputation: 40329

How can I print out the Memory Address of a variable?

I'd like to print what's behind an &myVariable. I tried NSLog(&myIntVar); but it won't work.

Upvotes: 16

Views: 15627

Answers (3)

akanksha singh
akanksha singh

Reputation: 33

Nslog(@"%x",&myIntVar); %x is for long int or you can print like Nslog(@"%p",&myIntVar); %p is for pointer

Upvotes: 0

smorgan
smorgan

Reputation: 21579

The argument to NSLog needs to be an NSString, so you want

NSLog(@"%p", &myIntVar);

Upvotes: 42

Alnitak
Alnitak

Reputation: 339816

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.

Upvotes: 6

Related Questions