Which one .(dot) or -> ?

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

  1. larswik macrumors 68000

    Joined:
    Sep 8, 2006
    #1
    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).

    (*myPtr).number

    or

    myPtr->number

    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.

    -Lars
     
  2. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
  3. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #3
    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.

    -Lee
     
  4. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
  5. chrono1081 macrumors 604

    chrono1081

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

Share This Page