Why does C have a distinction between -> and .?

前端 未结 7 1689
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-28 09:56

OK, this is of no serious consequence, but it\'s been bugging me for a while: Is there a reason for the distinction between the -> and . operato

7条回答
  •  清歌不尽
    2020-11-28 10:11

    Well, there could definitely be cases where you have something complex like:

    (*item)->elem
    

    (which I have had happen in some programs), and if you wrote something like

    item.elem
    

    meaning the above, it could be confusing whether elem is an element of struct item, or an element of a struct that item points to, or an element of a struct that is pointed to be an element in a list that is pointed to by an iterator item, and so on and so forth.

    So yeah, it does make things somewhat clearer when using pointers to pointers to structs, &c.

提交回复
热议问题