c - 无法使用 ifdef 宏初始化结构内部的数组

标签 c struct macros c-preprocessor

我正在尝试初始化下面的结构,称为 arduino_lcd_t 。因为我使用的外设可以一次写入四位数据,也可以一次写入八位数据,所以我决定使用一个宏来相应地定义自己。但是,我无法编译此代码,可能是因为 #ifdef的宏。我收到警告

declaration does not declare anything

是我哪里做得不对,还是不能这样做?相关代码如下。

#define FOUR_BIT_MODE 1

#ifdef FOUR_BIT_MODE
#define DATA_LEN 4;
#else
#define DATA_LEN 8
#endif

typedef struct
{
    long lighting_pin;
    long register_select_pin;
    long read_write_pin;
    long enable_pin;
    **long data_pins[DATA_LEN];**
} arduino_lcd_t;

最佳答案

long data_pins[DATA_LEN]; 

当宏被扩展时,它将变成

long data_pins[4;]; 

4; 不能是数组的索引。删除宏末尾的 ;

关于c - 无法使用 ifdef 宏初始化结构内部的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41774549/

相关文章:

c - 如何在结构数组中初始化数组?

c - 将 Zed Shaw 的调试宏移植到 MSVC

c - 三元运算符的执行

c - 如何在不显式链接数学库的情况下使用带有 gcc 的 c 中的数学函数

php - Pycrc 和 PHP 代码的 CRC32 计算与预期结果不匹配

c - 如何在结构中使用结构

c - 与类对象宏同名的类函数宏

c - 当我扫描二维数组时,它没有扫描所有值

c - ' {' 之前的预期表达式与互斥体初始化

C 自由结构体指针