C:开关中需要整数表达式,而不是XY

C:开关中需要整数表达式,而不是XY,c,pointers,enums,switch-statement,structure,C,Pointers,Enums,Switch Statement,Structure,我在开关和结构方面有一个小问题 我列举了所有的说明。该命令存储在Instr结构中(节点地址上有3个指针-作为三个地址代码) 但是现在,编译器开始抱怨开关值 开关代码如下所示: instrStack *instrStack; // Pointer on instruction struct Instr *instruction; // Taking the first instruction from the instruction stack instructio

我在开关和结构方面有一个小问题

我列举了所有的说明。该命令存储在Instr结构中(节点地址上有3个指针-作为三个地址代码)

但是现在,编译器开始抱怨开关值

开关代码如下所示:

instrStack *instrStack;
    // Pointer on instruction
    struct Instr *instruction;
    // Taking the first instruction from the instruction stack
    instruction = instrStackTop(instrStack);

    while(instruction != NULL) {
        instruction = instrStackTop(instrStack);

        switch (instruction->type) {
            // BUILT-IN FUNCTIONS

            case  insIfj16readInt:
                if(instruction->Id3->inc == 1) {
                    if (instruction->Id3->data.type == var_int) {
                        instruction->Id3->data.value.intValue = readInt();

                    } else {
                        throwException(4,0,0);
                    }

                } else {
                    throwException(8, 0, 0);
                }
                break;
            case insIfj16readString:

         etc. etc. more code and so one.
因此,下面是一个问题:

“开关中需要整数表达式,而不是'InstrType*'”


我真的不知道为什么会发生这种情况。我在我的词法分析器上使用相同的“系统”和switch和enum(我只是更改自动机的状态),这没有问题。

在您的代码中,
指令->type
的类型是
InstrType*
。您还需要一个级别的取消引用

差不多

 switch ( *(instruction->type) )

应该做这项工作。

您是一个
InstrType*
(即一个指针),在一个上下文中,整数类型是预期的(枚举是
类型),这是无效的

在没有看到其余代码的情况下,我打赌
type
字段可能不需要指向
InstrType
(即a
InstrType*
)的指针,而只需要
InstrType

typedef struct Instr {
    BTSNode *Id1; 
    BTSNode *Id2; 
    BTSNode *Id3;
    InstrType type;
}Instr;

哦,是的。非常感谢!我强烈怀疑这会导致一个
SIGSEGV
,因为我怀疑代码实际上为
指令->类型分配了内存,并且可能会执行类似
指令->类型=insIfj16readInt;
@AndrewHenle的操作。虽然你可能是对的,但这是另一篇文章的讨论内容,不是吗呃,这里显示的代码应该可以解决OP的问题。@SouravGhosh是真的。您已经回答了OPs的问题并解决了眼前的问题。我只是怀疑还有更多问题,因此我发表了评论。@John,不客气。您也可以。:)这也是我的想法。就像编译器说的,
指令->键入
->
*指令->键入
。投票以简单的打字错误结束此操作。
typedef struct Instr {
    BTSNode *Id1; 
    BTSNode *Id2; 
    BTSNode *Id3;
    InstrType type;
}Instr;