Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Objective-C Basics (Retired) Pointers and Memory Pointers

Why not just refer to the variable itself? I don't understand the use for pointers.

If we use the pointer to refer to the address in which a variable stores a value, why not just refer to the variable itself?

2 Answers

Kevin Hamil
Kevin Hamil
8,203 Points

Here's an excerpt from a Treehouse blog post about Obj-C basics. I think it's one of the most understandable "why" explanations I've read about pointers vs variables. Hope this helps.

"Pointers are a part of regular C and are used because they are more efficient [than variables]. When we use pointers in our programs, we only need to store and copy a simple pointer, which is really just an address for a space in memory. [A pointer] is a relatively small piece of data. If we instead had to store and copy the data being pointed to [ie. a variable], we might very quickly run into problems of not having enough memory. For example, it’s much more efficient to simply point to the location for a large video file and use that pointer multiple times in code than to actually have to use all the data of that large video file every time we access it in code."

As the excerpt says, pointers simply allow you to place an object in memory in one place and just tell your program where to retrieve that data when it needs it. With a variable you would have to replicate the use of the same data every time your program needed it, thus unnecessarily duplicating memory used for the same data.

100% clarified! Thank you very much, Kevin!