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 trialRodolfo Ocampo
1,875 PointsWhy 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
8,203 PointsHere'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.
Rodolfo Ocampo
1,875 Points100% clarified! Thank you very much, Kevin!