Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Object-Oriented Objective-C Tying it All Together Pointers in Practice

Ryan Jin
Ryan Jin
15,337 Points

Is *pointer the memory address of the pointer, or is it the memory address of the variable?

In the following code, the comments will be the question

NSString *myString = @"Hello World"; // Is this variable a pointer, or a NSString?
NSLog("The memory address for this string is %p \n", &myString); // Does this address represent the address of the pointer, or the address of the NSString?

2 Answers

Ruggiero A
Ruggiero A
8,534 Points

A pointer just any variable, has an address (his own memory address) and has a value (which in this case is going to be another memory address). If you do

NSLog(@"Address: %p \n", myString);

you're printing the address of the string which myString points to

If you do instead

NSLog(@"Address: %p \n", &myString);

you're not printing the address of the string, but the address of the pointer which points to the string. Hope I clarified it to you

Ryan Jin
Ryan Jin
15,337 Points

So technically declaring a pointer means putting two different things inside the memotry?

Ryan Jin
Ryan Jin
15,337 Points

Then what is the point of a pointer if it simply points to that address instead of being the object at that address?

Akash Sharma
seal-mask
.a{fill-rule:evenodd;}techdegree
Akash Sharma
Full Stack JavaScript Techdegree Student 14,147 Points

Ryan Jin because then I can change the object that the pointer is pointing through like int to float to some created object. Also they are different levels of memory and some variables in methods only exist in the level of the stack, which will be flushed out (all reserved memory will be freed) but if you alloc an object you will create the object in the level of the heap, which will last throughout the life of program not just 0.002 seconds in the method you called for instance.