Difference between -> and . in a struct?

前端 未结 7 1981
夕颜
夕颜 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:42

    You use the different notation according to whether the left-hand side is a object or a pointer.

    // correct:
    struct account myAccount;
    myAccount.account_number;
    
    // also correct:
    struct account* pMyAccount;
    pMyAccount->account_number;
    
    // also, also correct
    (*pMyAccount).account_number;
    
    // incorrect:
    myAccount->account_number;
    pMyAccount.account_number;
    
    0 讨论(0)
提交回复
热议问题