【问题标题】:error: request for member "machine" in something not a structure or union错误:请求成员“机器”不是结构或联合
【发布时间】:2013-04-16 06:26:01
【问题描述】:

当我尝试编译下面的程序时,我收到错误,因为请求成员“机器”不是结构或联合

struct machine
{
    int a;
    int b;
    int c;
};
struct config
{
    struct machine *machine;
    int n;
};
int main()
{
    struct config *conf;
    struct machine *mach;

    mach->a=1;
    mach->b=2;
    mach->c=3;

    conf.machine=mach; /* error in this line */  

return 0;
}

谁能帮我修复这个错误..提前谢谢!!!

【问题讨论】:

  • 使用struct config *conf;,您只需定义来自config conf 类型的指针,但您必须为其分配内存。 struct machine 也是如此。使用 malloc。

标签: pointers struct


【解决方案1】:

conf 是一个指针,而不是一个结构,所以你必须取消引用它,就像你对 mach 所做的那样。

conf->machine = mach;

另外,您需要为confmach 分配内存。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多