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) Introduction to Objective-C From Structs to Objects

why is ball a pointer (*ball)? Is it not possible to just use a variable ball?

ball is declared as *ball. Does this mean it is a pointer? When are pointers used and when are normal variables used?

2 Answers

Andrew Boryk
Andrew Boryk
15,916 Points

Points are used to send the value of variable to an address in memory. The reason why you must have a point on Ball is because Ball is an Object, and all objects need to have a reference point in memory. Primitive data types don't require pointers, however you may use them. Objective-C is an object oriented language, so even Strings (which in other languages wouldn't be objects), are actually objects and are declared with NSString *string. Hope I helped, been programming in Objective-C for a little over two years. I could be off a bit, but that is what I've always told myself.

i like it, thats what ill tell myself too.

To add, you need to use pointers for objects because they are initialized in the heap. Primitives are on the stack, which make them local, so you don't need pointers for them.

Michael Hulet
Michael Hulet
47,842 Points

It's been a while since I've done iOS development (I haven't actually taken the Treehouse track, but I've done a very little in the past), but I think all objects in Objective-C are pointed to, and I think it has to do with the fact that they have setter and getter methods that you can modify, and those actually store the variable

i see, thanks michael!