Using arrow -> and dot . operators together in C

后端 未结 5 1431
灰色年华
灰色年华 2021-02-01 07:51

I was under the impression that it was possible to access data from a sub-node of a linked list or similar structure by using the arrow and dot operators together like so:

5条回答
  •  被撕碎了的回忆
    2021-02-01 08:27

    Use -> for pointers; use . for objects.

    In your specific case you want

    if (sample->left->num > sample->right->num)
    

    because all of sample, sample->left, and sample->right are pointers.

    If you convert any of those pointers in the pointed to object; use . instead

    struct a copyright;
    copyright = *(sample->right);
    // if (sample->left->num > copyright.num)
    if (*(sample->left).num > copyright.num)
    

提交回复
热议问题