这个错误意味着什么,有什么简单的解释吗?
request for member '*******' in something not a structure or union
在我学习C语言的这段时间里,我已经遇到过它好几次了,但我对它的意思一无所知.
这个错误意味着什么,有什么简单的解释吗?
request for member '*******' in something not a structure or union
在我学习C语言的这段时间里,我已经遇到过它好几次了,但我对它的意思一无所知.
如果您试图在有指针的情况下访问实例,也会发生这种情况,反之亦然:
struct foo
{
int x, y, z;
};
struct foo a, *b = &a;
b.x = 12; /* This will generate the error, should be b->x or (*b).x */
正如在 comments 中指出的,如果有人用typedef
来表示指针,也就是说,在typedef中包含*
,这可能会让人感到非常痛苦,比如:
typedef struct foo* Foo;
因为你会得到looks个代码,就像它在处理实例,而实际上它在处理指针:
Foo a_foo = get_a_brand_new_foo();
a_foo->field = FANTASTIC_VALUE;
注意上面的代码看起来好像应该写成a_foo.field
,但是这会失败,因为Foo
是一个指向struct的指针.我强烈推荐against,typedef
:ED C中的指针.指针很重要,不要隐藏星号.让他们发光吧.