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 Objective-C Basics (Retired) Pointers and Memory Pointer Power

filibertoheiras
filibertoheiras
3,211 Points

Why use pointers?

What is the meaning to use pointers instead simple variables? Is for memory reduction, speed or what? I assume is almost the same.

For example is the same: char example[] = "example"; char letter = example[2]; //should be 'a' char *pletter = &example[2]; //also be 'a'

3 Answers

Stone Preston
Stone Preston
42,016 Points

there are many reasons, however a main one is memory. you can pass around variables in code which involves passing a copy of the WHOLE variable (passing by value), which could be a large chunk of memory and could take time. With pointers, all you are passing is the memory address (passing by reference), which is tiny. So if you had a very large object you wouldnt want to pass a copy of that, you would use a pointer and pass that instead since all it contains is the memory address, not the actual object

filibertoheiras
filibertoheiras
3,211 Points

Thanks Stone!! I assume when dealing with 3d applications with huge amount of data or something else should be mandatory to use pointers.. Is now clear, thanks again!!

Stone Preston
Stone Preston
42,016 Points

yes. and when working with objects in objective c you MUST use pointers as all objects have to be declared as pointers. So when doing ios development you use pointers almost exclusively.