John
John

Reputation: 67

C: Integer expression is required in switch instead of XY

I have a small problem with switch and structure.

I have an enum of all instructions. This recond in storaged in Instr struct (with the 3 pointers on node adress- as a three adress code)

typedef enum {
    // BUILT-IN FUNCTIONS
    insIfj16readInt,
    insIfj16readDouble,
    insIfj16readString,
    insIfj16lenght,
    insIfj16substr,
    insIfj16compare,
    insIfj16find,
    insIfj16sort,
    insIfj16print,
    //
    // MATH
    insPlus, 
    insMinus, 
    insMux, 
    insDiv, 
    //
    //COMPARE
    insEqual, 
    insNotEqual, 
    insLess, 
    insLessOrEqual, 
    insGreater, 
    insGreaterOrEqual, 
    insAssignment,
    insFunctionCall
}InstrType;

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

But now, the compiler started complaining about switch values.

Switch code is like this:

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.

So here is the compiler complain:

"Integer expression is required in switch instead of 'InstrType *'

I really don't know why is this happening. I'm using same "system" with switch and enum on my lexical analyzer (I just changing states of automat) and there is no problem with this.

Upvotes: 2

Views: 108

Answers (2)

dbush
dbush

Reputation: 223992

You're a InstrType * (i.e. a pointer) in a context where a integer type is expected (which an enum is), which is not valid.

Without seeing the rest of your code, I'm betting that the type field likely doesn't need a pointer to a InstrType (i.e. a InstrType *), just a InstrType.

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

Upvotes: 3

Sourav Ghosh
Sourav Ghosh

Reputation: 134336

In your code, instruction->type is of type InstrType *. You need one more level of dereference.

Something like

 switch ( *(instruction->type) )

should do the job.

Upvotes: 4

Related Questions