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

What does the "*" mean?

in the lesson "from structs to objects" he declares an object ball, from the sphere class like this Sphere *ball = [[Sphere alloc] init]; i don't understand the use of the *. in C, i know that its a pointer, but don't understand how it's being used here..

3 Answers

Yes, it is still a pointer. From your example, you have allocated an instance of the Sphere object to a memory location pointed-to by the ball pointer. The alloc method, after preprocessing, will produce a bunch of codes that includes something similar to malloc, for which the size needed to be allocated has been encapsulated (hidden) by the method and class declaration.

In most cases, anything true about C is also true about Objective-C as Objective-C is simply an extension of the C language. Soon you'll also encounter the & character, and it is used exactly the same way as in C.

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

I wrote briefly about this in a blog post a few months ago. The explanation there might help: http://blog.teamtreehouse.com/the-beginners-guide-to-objective-c-language-and-variables

Excerpt:

The asterisk (*) can be a little confusing. This is not part of the variable name. It can be placed anywhere between the data type and the variable name, so the following are all equivalent:

NSString* title;
NSString * title; 
NSString *title;

The * symbol is actually an operator that is used to de-reference a pointer. De-what a what now? Pointers are pretty much what they sound like. They “point” to a location in memory where the actual data is stored. That location in memory is the true container that holds the data.

Pointers are a part of regular C and are used because they are more efficient. 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. It’s a relatively small piece of data. If we instead had to store and copy the data being pointed to, 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.

Okay, so back to the asterisk: what does it mean to de-reference a pointer? It simply means that we obtain the value stored in the memory where the pointer is pointing to.

So is it wrong to declare it as Sphere ball = [[Sphere alloc] init]? Like i don't understand, what's stopping me?

Ben Jakuben
Ben Jakuben
Treehouse Teacher

It's confusing! You can't do that because Sphere is a complex data type. The only variables that you can declare without the asterisk are primitive data types like int, BOOL, etc.

All objects must be referenced through a pointer, and we declare pointers by including the asterisk. So in your example, "ball" would be a pointer to a location in memory where an instance of the Sphere class is stored. You would need the asterisk:

Sphere *ball = [[Sphere alloc] init];

Why is it like this? One reason is what I pointed out in that excerpt from my blog post. Simple data types require smaller memory and are easier to work with. More complex data can require a LOT more memory, and our programs can run more efficiently by manipulating pointers to that one memory location rather than copying it around multiple times for whatever we need it for.