簡體   English   中英

C編程:訪問聯合中的指針

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM