首页 > 解决方案 > C“'struct.pointer''.'中的结构错误中的指针 出乎意料的“

问题描述

得到了这个结构:

typedef struct {
    unsigned long index;

    void(*function)();
} program;

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

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

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 类型,所以我添加了它

标签: cpointersgccstruct

解决方案


当您使用 write typedef structprogram 时,成为结构的名称,而不是实例。只需删除typedef它就可以了。


推荐阅读