lampShade
lampShade

Reputation: 4391

What does this ' ->' mean in c/objective-c?

I'm looking over some code and I came across some syntax that I don't know the meaning of. What does the '->' mean?

-(void) getTransformValues:(struct transformValues_*) tv
{
    tv->pos         = positionInPixels_;
    tv->scale.x     = scaleX_;
    tv->scale.y     = scaleY_;
    tv->rotation    = rotation_;
    tv->skew.x      = skewX_;
    tv->skew.y      = skewY_;
    tv->ap          = anchorPointInPixels_;
    tv->visible     = visible_;
}

Upvotes: 1

Views: 1769

Answers (7)

Baltasarq
Baltasarq

Reputation: 12212

The arrow operator ('->') is used in the same place you would use the dot operator ('.'), but with a pointer to a structure instead of an object of that structure.

typedef struct _Person {
    char name[200];
    unsigned int age;
} Person;

If you created an object of that structure, you would use the dot operator in order to access its members:

int main()
{
    Person p1;

    strcpy( p1.name, "Baltasar" );
    p1.age = 36;
}

However, if you a pointer to a structure, instead of the structure itself, you could only use the arrow operator, or a little bit more complex dot operator:

int main()
{
    Person p1;
    Person *ptrPerson = &p1;    // ptrPerson points to p1

    strcpy( ptrPerson->name, "Baltasar" );
    ptrPerson->age = 36;
}

As I said above, you could still use the dot operator:

 int main()
    {
        Person p1;
        Person *ptrPerson = &p1;    // ptrPerson points to p1

        strcpy( (*ptrPerson).name, "Baltasar" );
        (*ptrPerson).age = 36;
    }

Of course, all of this discussion involves a lot more topics, such as pointers, the heap, etc. Hope this helps.

Upvotes: 12

Anders
Anders

Reputation: 290

Hmmmm did you at least consider trying to find it out for yourself before posting here? This is what I got from searching operators....

Upvotes: -3

GWW
GWW

Reputation: 44093

It's used to access a member of an object / struct pointed to by a variable.

For example tv->pos is used to access the member variable pos from the object pointed to by tv

Upvotes: 2

Oleiade
Oleiade

Reputation: 6274

"->" is used in order to access a struct pointer element. In C at least...

typedef struct test {
   int    one;
   int    two;
}              t_test;


t_test    *foo;
/* Allocation and all the stuff */
foo->one = ...
foo->two = ...

Upvotes: 1

Jonathan Wood
Jonathan Wood

Reputation: 67193

-> is used to mean the same thing as the dot (which means to access a member of a structure, class, or union), except that -> is used when the variable is a pointer.

Upvotes: 1

Brian
Brian

Reputation: 8357

The arrow operator (->) takes a struct pointer (to a transformValues_ in this case), dereferences it, then accesses that member variable. IE: these are equivelant:

 (* tv).pos === tv->pos

Upvotes: 0

Ed Swangren
Ed Swangren

Reputation: 124642

The -> symbol is used to access a member of a pointer type. It is the same as dereferencing the pointer and using the dot operator, i.e.,

(*tv).pos = positionInPixels_;

Upvotes: 3

Related Questions