2017-09-26 47 views
0

有人可以幫我訪問一個聯合中的指針,我不斷得到一個[錯誤]無效類型參數' - >'(有'結構節點')。下面是我的數據結構的片段在裏面:C編程:訪問一個聯盟中的指針

typedef enum{LEAF,INODE}indicator; 

typedef struct twoThree{ 
    indicator indic; 
    union{ 
     struct L{ 
      int key; 
     }leaf; 
     struct node{ 
      int key1,key2; 
      struct twoThree *LC,*MC,*RC; 
     }iNode; 
    }U; 
}*TTT; 


void insertElem(TTT *T, int elem) 
{ 
    TTT *temp; 

    if(*T==NULL){ 
     *T=initTree(); 
     (*T)->indic = LEAF; 
     (*T)->U.leaf.key = elem; 
    }else if((*T)->indic == LEAF){ 
     if(elem < (*T)->U.leaf.key){ 
      (*temp)=initTree(); 
      (*temp)->indic = INODE; 
      (*temp)->U.iNode.key1 = elem; 

      **(*temp)->U.iNode->LC = *T; /*This is my problem"->LC" part*/** 
     } 
    } 
} 

TTT initTree() 
{ 
    TTT T; 
    T=(TTT)malloc(sizeof(struct twoThree)); 
    if(T!=NULL){ 
     printf("Initialization of tree was successful.\n"); 
    }else{ 
     printf("Failed initialization of tree.\n"); 
    } 

    return T; 
} 

如果任何人都可以對我如何在聯盟內訪問我的指針指出,這將是巨大的。多謝你們。

+0

您有多個錯誤。對於初學者來說,當您解除引用時,「temp」指向何處? –

+2

'typedef struct twoThree {...} * TTT' - 在大多數情況下'typedef'指針是個不錯的主意。 –

+0

至於你的問題,'U.iNode'不是一個指向結構的指針,它是一個結構*對象*,因此你不應該使用箭頭運算符' - >'來訪問它的成員。 –

回答

0
else if((*T)->indic == LEAF){ 
     if(elem < (*T)->U.leaf.key){ 
      (*temp)=initTree(); 
      (*temp)->indic = INODE; 
      (*temp)->U.iNode.key1 = elem; 

      **(*temp)->U.iNode->LC = *T; /*This is my problem"->LC" part*/** 
     } 

在本節中存在結構不匹配,因此它顯示了錯誤。

說明: T和temp指向了兩個結構,在elseif情況下(* T) - > U.leaf.key被訪問,這意味着結構包括(指示器指示和結構葉)。 在相同的情況下(* temp) - > U.iNode.key1被訪問,這意味着臨時點指向類型結構(指示器指示和結構iNode)。這種不匹配是錯誤的原因。由於聯盟一次只允許存在結構iNode或結構葉。