What is "->" in Objective C?

I've seen this operator pop up quite a few times in example code in "Learn Objective C on the Mac."

I believe it's an operator in the C language which Objective C inherits. I tried Googling and searching Stack Overflow and oddly nothing came up.

Does it have an English name?


It has to do with structures.

When we have a struct available locally on the stack, we access its members with the . operator. For example:

CGPoint p = CGPointMake(42,42);
NSLog(@"%f", p.x);

However, if we instead have a pointer to a structure, we have to use the -> operator:

CGPoint *p = malloc(1*sizeof(CGPoint));
p->x = 42.0f;
NSLog(@"%f", p->x);
free(p);

-> is not specific to Objective-C. It's a C operator.

Now that's cleared, it's the member access operator, equivalent to a pointer dereference and then using the dot operator on the result.

Say you had a struct like this:

typedef struct Person {
   char *name;
} Person;

Person *jacob = malloc(1*sizeof(Person));

So this statement:

jacob->name = "Jacob";

Is equivalent to this statement:

(*jacob).name = "Jacob";

Of course, don't forget the free:

free(jacob); 

In C

a->b

is a shortcut for

(*a).b

which is for dereferencing of members of a struct that is pointed to.

This is useful, because of . binds stronger than the dereferencing operator * . So by using -> you avoid having to use these ugly parentheses.


It's a member selection (or access) equivalent to a pointer de-reference (as pointed out in comments)

a->member is equivalent to (*a).member in C/C++