繁体   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