簡體   English   中英

在另一個結構中初始化和使用結構成員

[英]Initializing and using struct members inside another struct

我需要在新的struct(struct2)中使用先前聲明的struct(struct1)。 我也想對其進行初始化,並使用struct1的某些成員來初始化struct2的其他成員。

具體來說,我想為struct1設置值並使用其中一些值來定義struct2其他成員的大小

我嘗試了代碼中顯示的內容,但是我不明白為什么它不起作用。

typedef struct ytxModule{
    uint8_t nEncoders;
    uint8_t nDigital;
    uint8_t nAnalog;
    uint8_t nLedsPerControl;
};

typedef struct{
    ytxModule components = {4, 0, 0, 16};

    // encoder pin connections to MCP23S17
    uint8_t encPins[components.nEncoders][2] = {
      {1, 0},  
      {4, 3},   
      {14, 15},  
      {11, 12}   
    };
    // buttons on each encoder
    uint8_t encSwitchPins[components.nEncoders] = { 2, 5, 13, 10 }; 

}ytxE41Module;

我收到的錯誤消息是:

sketch\headers/modules.h:52:37: error: invalid use of non-static data member '<anonymous struct>::components'

  ytxModule components = {4, 0, 0, 16};

                                     ^

sketch\headers/modules.h:55:18: error: from this location

  uint8_t encPins[components.nEncoders][2] = {

任何幫助將不勝感激 :)

最小的必要更改

您不能將typedef與初始化程序混合使用-至少不能在C語言中使用。您也不能擁有結構類型,其大小根據結構中的數據而有所不同-至少不能不使用一個(並且只有一個)靈活數組成員(FAM),但是您的代碼嘗試使用兩個變量數組(因此它們不能是FAM)。

您需要更多類似的東西:

typedef struct ytxModule{
    uint8_t nEncoders;
    uint8_t nDigital;
    uint8_t nAnalog;
    uint8_t nLedsPerControl;
} ytxModule;  // Add name for typedef

typedef struct{
    ytxModule components;
    uint8_t encPins[4][2];
    uint8_t encSwitchPins[4]; 
} ytxE41Module;

ytxE41Module module = {
    .components = {4, 0, 0, 16},
    .encPins = {
      {1, 0},  
      {4, 3},   
      {14, 15},  
      {11, 12}   
    },
    .encSwitchPins = { 2, 5, 13, 10 },
}; 

如果將.components.nEncoders初始化為4以外的其他值,則形狀不會改變。 根據您的需要,您可以考慮用一個可在編譯時更改的值替換硬編碼的4 ,但是您還需要一種方法來調整初始值設定項數組的大小以使其匹配,這可能是最好的,但最好的情況是難以理解的。 目前尚不清楚如何建立初始值。

使用靈活的數組成員

如果要使用靈活的數組成員,則必須組織一個類型來保存encSwitchPinsencPins數據的數組,並使用不同的符號訪問FAM的元素。

typedef struct ytxModule{
    uint8_t nEncoders;
    uint8_t nDigital;
    uint8_t nAnalog;
    uint8_t nLedsPerControl;
} ytxModule;

typedef struct
{
    ytxModule components;
    struct
    {
        uint8_t encPins[2];
        uint8_t encSwitchPins;
    } pins[]; 
} ytxE41Module;

您不能再為該數據類型使用初始化程序; 您將動態分配內存,然后為分配的數據分配值:

// Data used for initializing structure
int n_pins = 4;
uint8_t encPins[][2] = {
    {  1,  0 },  
    {  4,  3 },   
    { 14, 15 },  
    { 11, 12 },   
};
uint8_t switchPins[] = { 2, 5, 13, 10 };

// Allocate and check allocation
ytxE41Module *mp = malloc(sizeof(*mp) + n_pins * sizeof(mp->pins[0]));
if (mp == NULL)
    return NULL;

// Assign values to allocated structure
mp->components = (ytxModule){ n_pins, 0, 0, 16};
for (int i = 0; i < n_pins; i++)
{
    for (int j = 0; j < 2; j++)
        mp->pins[i].encPins[j] = encPins[i][j];
    mp->pins[i].encSwitchPins = switchPins[i];
}

如果標記嵌入式結構類型,則可以初始化該類型的數組,然后使用memmove() (或memcpy() )將單獨的數組復制到FAM結構中。 等等,如果結構中的數據量是固定的(例如,數組中的4個元素),則要簡單得多,如答案的第一部分所示。

暫無
暫無

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

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