“在非结构或联合的东西中请求成员 '*******'"是什么意思?意思是? [英] What does "request for member '*******' in something not a structure or union" mean?

查看:29
本文介绍了“在非结构或联合的东西中请求成员 '*******'"是什么意思?意思是?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于这个错误的含义是否有一个简单的解释?

Is there an easy explanation for what this error means?

request for member '*******' in something not a structure or union

在我学习 C 的过程中,我曾多次遇到它,但我不知道它的含义.

I've encountered it several times in the time that I've been learning C, but I haven't got a clue as to what it means.

推荐答案

如果您有指针时尝试访问实例也会发生这种情况,反之亦然:

It also happens if you're trying to access an instance when you have a pointer, and vice versa:

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 */

正如评论中所指出的,如果有人去使用 typedefsa 指针,即在 typedef 中包含 * ,这可能会令人难以忍受,如下所示:

As pointed out in a comment, this can be made excruciating if someone goes and typedefs a pointer, i.e. includes the * in a typedef, like so:

typedef struct foo* Foo;

因为这样你得到的代码看起来就像在处理实例,而实际上它在处理指针:

Because then you get code that looks like it's dealing with instances, when in fact it's dealing with pointers:

Foo a_foo = get_a_brand_new_foo();
a_foo->field = FANTASTIC_VALUE;

注意上面的内容看起来好像应该写成a_foo.field,但是由于Foo 是一个指向struct 的指针,所以这会失败.我强烈推荐反对 typedef:ed 指针在 C 中.指针很重要,不要隐藏你的星号.让它们发光.

Note how the above looks as if it should be written a_foo.field, but that would fail since Foo is a pointer to struct. I strongly recommend against typedef:ed pointers in C. Pointers are important, don't hide your asterisks. Let them shine.

这篇关于“在非结构或联合的东西中请求成员 '*******'"是什么意思?意思是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆