Which one .(dot) or -> ?

Discussion in 'Mac Programming' started by larswik, Jan 2, 2011.

  1. larswik macrumors 68000

    Sep 8, 2006
    The book gave me a choice as to which one to use to access a field in a Struct after I passed a pointer of the struct as a parameter (I hope I said all that right).




    It says both are the same but I remember looking over a little Objective C code and I saw people using the (dot). Will both of these work on Objective-C since it is built on top of C or use the (dot) version if my next step is Object-C? The book said more people programing in C use this version myPtr->number.

  2. balamw Moderator


    Staff Member

    Aug 16, 2005
    New England
  3. lee1210 macrumors 68040


    Jan 10, 2005
    Dallas, TX
    Only worry about C right now. All C is Objective-C. Structs behave exactly the same. With an object in Objective-C there is a . syntax for accessing properties, which is just syntactic sugar for a message pass. None of that is relevant to you right now. When you have a struct the . operator gets you a member. When you have a pointer to a struct -> gets you a member. * will dereference a pointer to a struct so you have a struct so you can use . to access a member.

    If you have a pointer to a struct you can dereference and dot or arrow. You'll get the same result.

  4. larswik thread starter macrumors 68000

    Sep 8, 2006
  5. chrono1081 macrumors 604


    Jan 26, 2008
    Isla Nublar
    For the sake of any programmers reading your code use myPtr->number :D

Share This Page