struct中的struct元素的部分初始化不起作用(Arduino)

时间:2019-06-21 03:19:52

标签: c arrays struct arduino arduino-ide

免责声明-这是与 arduino 相关的问题。

我无法初始化在另一个结构中声明的结构的元素。

我的结构如下:

struct finger
{
  int pin;
  bool upper_joint;
  bool lower_joint;
};

struct Glove {
  struct finger index;
  struct finger middle;
  struct finger ring;
  struct finger pinkie;
  struct finger thumb;
};

//Initializing the array
struct Glove glove = { .index.pin = 20, .middle.pin = 22, .ring.pin = 24, .thumb.pin = 26};

我要做的是在声明手套结构时,仅在结构 finger 中初始化 pin 变量。

但是,我收到一条错误消息:

expected primary-expression before '.' token

我的完整错误消息:

Arduino: 1.8.9 (Windows Store 1.8.21.0) (Windows 10), Board: "Arduino/Genuino Uno"

Sensor_Glove:40:24: error: expected primary-expression before '.' token

 struct Glove glove = { .index.pin = 20, .middle.pin = 22, .ring.pin = 24, .thumb.pin = 26};

                        ^

Sensor_Glove:40:41: error: expected primary-expression before '.' token

 struct Glove glove = { .index.pin = 20, .middle.pin = 22, .ring.pin = 24, .thumb.pin = 26};

                                         ^

Sensor_Glove:40:59: error: expected primary-expression before '.' token

 struct Glove glove = { .index.pin = 20, .middle.pin = 22, .ring.pin = 24, .thumb.pin = 26};

                                                           ^

Sensor_Glove:40:75: error: expected primary-expression before '.' token

 struct Glove glove = { .index.pin = 20, .middle.pin = 22, .ring.pin = 24, .thumb.pin = 26};

                                                                           ^

exit status 1
expected primary-expression before '.' token

1 个答案:

答案 0 :(得分:2)

您使用的语法对于嵌套struct的初始化无效。有关有效语法,请参见here->嵌套初始化部分。

以下是您要实现的目标的有效替代方法。

使用指示符...

struct Glove glove = {                                                                                 
    .index = {
        .pin = 20
    },                                                                                                 
    .middle = {                                                                                        
        .pin = 22                                                                                      
    },
    .ring = {
        .pin = 24 
    },
    .thumb = {
        .pin = 26                                                                                      
    },                                                                                                 
};

依靠struct元素的声明顺序...

struct Glove glove3 = {
    {20}, // index - pin 
    {22}, // middle - pin                                                                              
    {24}, // ring - pin                                                                                
    {0}, // pinkie - pin                                                                               
    {26}, // thumb - pin                                                                               
};

为外部元素使用标识符,为内部元素使用声明顺序...

struct Glove glove2 = {                                                                                
    .index = {20}, // pin = 20                                                                         
    .middle = {22}, // pin = 22                                                                        
    .ring = {24}, // pin = 24                                                                          
    .thumb = {26}, // pin = 26                                                                         
    };

不一致...

struct Glove glove4 = {
    .index = {20}, // index.pin = 20                                                                   
    {22}, // middle.pin = 22                                                                           
    // Since we are using designators we can change the order                                          
    .thumb = {26}, // thumb.pin = 26                                                                   
    .pinkie = {.pin = 24}, // pinkie.pin = 24                                                          
    };