[英]C programming: Accessing pointers in a Union
有人可以帮我用联合访问指针吗,我不断收到[错误]无效类型参数'->'(具有'struct node')。 这是其中包含我的数据结构的代码段:
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;
}
如果有人能指出我如何在联合体内访问指针,那将很棒。 多谢你们。
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被访问,这意味着该结构包括(指示符indic和struct leaf)。 在相同情况下,将访问(* temp)-> U.iNode.key1,这意味着温度指向类型的结构(指示符indic和struct iNode)。 此不匹配是导致错误的原因。 由于联合将允许一次仅存在struct iNode或struct leaf。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.