簡體   English   中英

如何將位於結構數組內的一個指針內容復制到另一個指針

[英]How to copy one pointer content located inside an array of structs to another pointer

我有以下列表,一種是用連接結構制作的包裝,另一種是用靜態數組制作的卡車。

typedef struct
{
    int id_pack;
    int weight_kg;
    int value;
    int priority;           //low 0, high 1
    struct pack* next;
} pack;

typedef struct pack head;  //declaring a struct head of type pack which is the head of a list of packs

typedef struct
{
    int id_truck;
    head* p_head;            //pointer to the list of packs of each truck
} truck;

int main()
{
    truck dogana[N];        //list of trucks made with a static array
}

現在,我要創建一個函數來檢查第一輛卡車的裝箱清單中至少一個裝箱的優先級是否為高優先級(1)。 為了做到這一點,我想到了一個臨時指針,以避免丟失對包列表開頭的引用(我需要在其他其他事情中使用它,我必須在函數內部執行此操作); 這是我的職責。

void uscita_dogana (truck dogana[])
{
    head* temp;
    temp = dogana[0].p_head; // I use temp to avoid losing the head 

    while (temp != NULL)   //cicle to run trough the packs
    {
        if (temp->priority == 1) //check the priority of the pack
        {
           //INSTRUCTIONS
        }
    }
}

現在,編譯器在temp-> priority == 1時給了我一個錯誤,該錯誤為:“取消引用不完整類型'head {aka struct pack}的指針;

我嘗試了許多不同的方法來解決此問題,但沒有成功。 我尋求幫助,並在網站上進行了搜索,但找不到正確的解決方案,希望有人可以幫助我!

對於普遍的困惑,我感到抱歉,英語不是我的母語,我才剛剛開始編碼。 提前致謝!

您已經將類型pack定義為匿名結構的別名(請注意,在typedef struct { ... } pack ,關鍵字struct之后沒有標識符。

以后定義時

typedef struct pack head;

那么您指的是尚未定義的struct pack 編譯器會將此記錄為不完整的類型,因此head也將為不完整的類型。 這就是為什么不知道temp->priority temphead類型的temp->priority的原因。

typedef pack head;

它應該工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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