【问题标题】:Pointers inside struct error in C " 'struct.pointer' '.' unexpected " [closed]C“'struct.pointer''.'中的结构错误中的指针出乎意料的“ [关闭]
【发布时间】:2019-03-03 18:58:06
【问题描述】:

得到这个结构:

typedef struct {
    unsigned long index;

    void(*function)();
} program;

当我尝试访问第二个字段时,我得到了这个:

error: expected '=', ',', ';', 'asm' or '__attribute__' before '.' token in program.callNext = next

下一个是之前声明的函数:

void next() {
    // code
}

编辑:
完整代码:

typedef unsigned char bool;

#define true 1
#define false 0 

void next() {
    //useless code
}

typedef void(*function)();    

typedef struct {
    unsigned long index;
    bool running;

    function callNext;
} program;

program shell;

shell.running = true;
shell.index = 0;
shell.callNext = next;

gcc 无法识别 bool 类型,所以我添加了它

【问题讨论】:

  • 你有一个名为program的变量吗?
  • prog.c:9:5: error: unknown type name ‘bool’
  • @melpomene 在编辑段末尾解释
  • 错误信息与您发布的任何代码都不匹配。错误为program.function,但代码为shell.callNext。

标签: c pointers gcc struct


【解决方案1】:

当你使用 write typedef struct program 时,程序成为结构的名称,而不是实例。只需删除 typedef 即可。

【讨论】:

  • 我知道它应该是这样的,以便我可以实例化不同的程序
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-01
  • 1970-01-01
  • 2021-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多