簡體   English   中英

在另一個結構內創建一個結構數組並訪問 C 中數組內的結構成員

[英]Creating an array of struct inside another struct and accessing the members of the struct inside the array in C

我想在另一個結構(也用 typedef 定義)內創建結構(用 typedef 定義)數組並訪問數組內的結構成員,但我有點卡住了。 如果你能引導我走向正確的方向,我真的很感激。

我的代碼是一個更大代碼的一部分,所以我將嘗試用一個示例代碼來解釋我的情況。 如果它不理解,我可以隨時分享我的實際代碼。

我在 header 文件中有兩個結構;

#define MAX_COMP_COUNT 64

typedef struct Module Module;
typedef struct Component Component;

struct Module
{
    Component** _CompArr[MAX_COMP_COUNT];
    uint8_t _CompCount;

};

struct Component
{
    uint8_t _page, _id;
};

我想在 Module 結構中的數組內添加 Component 結構(目前不需要動態大小。)

所以我在一個.c文件中寫了這個function;

uint8_t AddComp(Module* mod, Component* _comp, uint8_t __page, uint8_t __id)
{
    //Pass the corresponding data from component to component struct
    _comp->_id = __id;
    _comp->_page = __page;

    //Add the component struct to the list on the Module Struct
    mod->_CompArr[mod->_CompCount] = &_comp;
    mod->_CompCount++;

    //Return OK
    return 0;
}

但是我試圖通過數組訪問結構的 id 成員,但我有點失敗。 我不知道我是否濫用了某些東西或上面的部分定義錯誤。 如果您能糾正我,那將非常有幫助。 提前致謝。

uint8_t Module_Update(Module *mod)
{
    for(uint8_t i = 0; i < mod->_CompCount; i++)
    {
        //This part is not working
        if(*(mod->_CompArr[i])->_id == 0xFF)
            //Do stuff
    }

    return 0;
}

編輯:我收到此錯誤;

錯誤:'*mod->_CompArr[(int)i]' 是一個指針; 你的意思是使用'->'嗎?

  • 刪除多層間接。 如果您只是想擁有一個固定大小的數組,那么請使用一個固定大小的數組。
  • 不要命名以下划線開頭的標識符,因為這樣的名稱是為編譯器保留的。

簡化,我們得到這個:

#include <stdint.h>

#define MAX_COMP_COUNT 64

typedef struct 
{
  uint8_t page;
  uint8_t id;
} Component;

typedef struct 
{
  Component CompArr [MAX_COMP_COUNT];
  uint8_t CompCount;
} Module;

然后相應地修改您的功能。 這是一個非常奇怪的 API 將 Component 作為指針傳遞,然后根據其他參數填充該 Component 結構的值 - 不要創建奇怪的 function 接口:而是假設 Component 已經由調用者填充:

void AddComp (Module* mod, Component* comp)
{
  //Add the component struct to the list on the Module Struct
  mod->CompArr[mod->CompCount] = comp;
  mod->CompCount++;
}

或者,如果動態分配是一個選項:

void AddComp (Module* mod, uint8_t page, uint8_t id)
{
  Component* comp = malloc(sizeof *comp);
  comp->page = page;
  comp->id   = id;

  mod->CompArr[mod->CompCount] = comp;
  mod->CompCount++;
}

現在,您有很多似乎不必要的間接負載:

struct Module
{
    Component **_CompArr[MAX_COMP_COUNT];
    uint8_t _CompCount;

};

Component **_CompArr中的二級指針沒有理由。 您可以刪除一級或二級間接:

  1. 使用單指針:

     struct Module { Component* _CompArr[MAX_COMP_COUNT]; uint8_t _CompCount; };

    您將在循環中使用結構作為

    if(mod->_CompArr[i]->_id == 0xFF)...

    當您分配它時,不再需要額外的參考:

     mod->_CompArr[mod->_CompCount] = _comp;
  2. 第二種選擇是完全取消間接。 這可能不太理想,因為它涉及復制數據,但它看起來像這樣:

     struct Module { Component _CompArr[MAX_COMP_COUNT]; uint8_t _CompCount; };

    用法:

     if(mod->_CompArr[i]._id == 0xFF)...

    和任務:

     mod->_CompArr[mod->_CompCount] = *_comp;

另一種選擇當然是修復你的編譯器錯誤。 由於優先規則, *(mod->_CompArr[i])->_id等價於*((mod->_CompArr[i])->_id)而不是(*(mod->_CompArr[i]))->_id 要獲得后者,您必須顯式包含括號,如圖所示。

暫無
暫無

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

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