使用方向 - >和点。运营商一起,用C [英] Using arrow -> and dot . operators together in C
本文介绍了使用方向 - >和点。运营商一起,用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是IM pression下,这是可以用箭头和点运营商一起,像这样从一个链表或类似结构的子节点访问数据:
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:
typedef struct a{
int num;
struct a *left;
struct a *right;
}tree;
tree *sample;
...
if(sample->left.num > sample->right.num)
//do something
但是当我试图实现这一点,使用 - >和。从子节点我得到的错误存取数据的东西为会员NUM要求不是一个结构或联合。
but when I try to implement this, using -> and . to access data from a sub node I get the error "request for member num in something not a structure or union".
推荐答案
使用 - >
为指针;使用。
的对象。
Use ->
for pointers; use .
for objects.
在特定的情况下,你希望
In your specific case you want
if (sample->left->num > sample->right->num)
由于所有的样品
,样品 - >将离开
和样品 - &GT ;右
是指针
如果你转换任何在这些尖指向的对象;使用。
而不是
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)
这篇关于使用方向 - >和点。运营商一起,用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文