[英]Preprocessor initialized struct
我有大量的常量數據需要存儲在頭文件中。 由於數據的性質(如何嵌套),我決定使用由預處理器初始化的結構。
我試圖像這樣初始化這些結構:
typedef struct s_PWM {
uint8_t muxmode;
char *name;
char *path;
} PWM;
typedef struct s_PIN {
char *name;
uint8_t gpio;
char *mux;
uint8_t eeprom;
PWM *pwm;
} PIN;
這可以讓我做類似的事情
PIN p1 = P8_19;
從某種意義上講,要組裝具有適當屬性的結構。
基本上,您可以將圖釘發送到函數中
pinMode(P8_19, OUTPUT)
這是有問題的行
#define P8_19 ((PIN){"EHRPWM2A", 22, "gpmc_ad8", 14, (PWM*){4, "EHRPWM2A", "ehrpwm.2:0"}})
這是警告
src/gpio.c:50:2: warning: excess elements in scalar initializer
src/gpio.c:50:2: warning: (near initialization for '(anonymous)')
src/gpio.c:50:2: warning: excess elements in scalar initializer
src/gpio.c:50:2: warning: (near initialization for '(anonymous)')
有誰知道為什么會這樣嗎? 我是否將PWM *結構錯誤地嵌套到PIN結構中?
簡短的回答:是的。
不用指針,您應該直接使用結構:
typedef struct s_PIN {
char *name;
uint8_t gpio;
char *mux;
uint8_t eeprom;
PWM pwm; /* Notice: not a pointer */
} PIN;
另外,您不需要進行轉換:
#define P8_19 {"EHRPWM2A", 22, "gpmc_ad8", 14, {4, "EHRPWM2A", "ehrpwm.2:0"}}
#define P8_19 ((PIN){"EHRPWM2A", 22, "gpmc_ad8", 14, (PWM*){4, "EHRPWM2A", "ehrpwm.2:0"}})
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
標記的部分不是指針。
我對這個問題的處理略有不同。 我將在自己的模塊中定義引腳名稱,如下所示。
gpio.h
:
typedef struct s_PWM {
uint8_t muxmode;
char *name;
char *path;
} PWM;
typedef struct s_PIN {
char *name;
uint8_t gpio;
char *mux;
uint8_t eeprom;
PWM *pwm;
} PIN;
...
extern PIN * P8_19;
gpio.c
:
PWM _P8_19_PWM = {4, "EHRPWM2A", "ehrpwm.2:0"};
PIN _P8_19 = {"EHRPWM2A", 22, "gpmc_ad8", 14, &_P8_19_PWM};
PIN *P8_19 = &_P8_19;
然后,可以通過包含gpio.h
並正常使用P8_19在另一個文件中引用它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.