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 Structs

Why a pointer for c and not for r

Just wondering in the last video, why in our function do we have a pointer for c but not for r?

2 Answers

Hi Greyson, that happens because the instructor plans to pass an array to the function. The 'r' doesn't hold an array of elements so you can pass it into the function as is, but since "c" is an array by using a pointer, you point to the address of the 1st element of the array with helps you get to the other elements as well if needed. That way when you select c[1] for example it knows where to access the value of the array stored in that position by calculating its offset from the 1st element of the array. I hope this kind of explains it.

Thank you. That is what I assumed. But then I checked my notes from a couple videos before when he went through explaining pointers for the first time and he used one for *lesson and it was being pointed to c that wasn't an array. Was he just doing this to show how pointers work? Or why was one needed in that case when it wasn't an array?

You can create pointer variables to point to addresses or create null pointers. Basically a pointer points to an address, it doesn't hold the array but points to the address of the array for example (specifically the 1st element usually.) I haven't seen that video to be honest, but if this doesn't cover it up you can show me the video so I can explain what the instructor is doing.