开发者

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_;
    t开发者_如何学编程v->visible     = visible_;
}


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.


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_;


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


-> 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.


"->" 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 = ...


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜