2016-11-25 75 views
1

我在開關和結構方面存在小問題。C:在開關中需要整數表達式而不是XY

我有一個枚舉的所有說明。此Recond(活化)在INSTR結構(與節點adress-作爲三ADRESS代碼3個指針)蓄能

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; 

但現在,編譯器開始抱怨開關值。

開關碼是這樣的:

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使用了相同的「系統」(I ju改變自動裝置的狀態)並且這沒有問題。

+0

就像編譯器說的那樣,'instruction-> type' - >'* instruction-> type'。投票結束這個簡單的錯字。 – Lundin

回答

3

在您的代碼中,instruction->type類型爲InstrType *。您需要更多級別的解除引用。

喜歡的東西

switch (*(instruction->type)) 

應該做的工作。

+1

哦,它確實。非常感謝! – John

+0

我強烈懷疑這會導致'SIGSEGV',因爲我懷疑代碼實際上是爲'instruction-> type'分配內存,並且可能做了類似'instruction-> type = insIfj16readInt;' –

+0

@AndrewHenle,而你可能是對的,那是另一篇文章的討論,不是嗎?根據此處顯示的代碼_,這應該解決OP的問題。 –

3

你是一個InstrType *(即一個指針)在上下文中的整數類型是預期的(其中enum是),這是無效的。

沒有看到你的代碼的其餘部分,我把賭注押在type領域可能並不需要一個指向InstrType(即一個InstrType *),只是一個InstrType

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

這也是我的想法。 – user3794667384