Difference between -> and . in a struct?

前端 未结 7 1994
夕颜
夕颜 2020-12-13 10:09

If I have a struct like

struct account {
   int account_number;
};

Then what\'s the difference between doing

myAccount.acco         


        
7条回答
  •  一个人的身影
    2020-12-13 10:28

    -> is a shorthand for (*x).field, where x is a pointer to a variable of type struct account, and field is a field in the struct, such as account_number.

    If you have a pointer to a struct, then saying

    accountp->account_number;
    

    is much more concise than

    (*accountp).account_number;
    

提交回复
热议问题