2017-09-26 70 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或结构叶。