Moh*_*efi 2 c directive c-preprocessor preprocessor-directive
我想知道是否可以将参数发送到#define宏以选择不同的输出
例如:
#define Row(1) LPC_GPIO0
#define Row(2) LPC_GPIO3
#define Row(3) LPC_GPIO2
Run Code Online (Sandbox Code Playgroud)
然后在我的代码中,我创建了一个发送参数的循环
Row(x)
Run Code Online (Sandbox Code Playgroud)
小智 5
此宏语法不存在.
而且,它不可能存在,因为宏在编译器编译代码之前被扩展.如果您x不是编译时常量,则永远不会有办法确定宏调用的源代码中要替换的内容.
如果需要索引某些值,只需使用数组,例如(假设这些常量是整数):
static int rows[] = { 0, LPC_GPIO0, LPC_GPIO3, LPC_GPIO2 };
Run Code Online (Sandbox Code Playgroud)
写作
rows[x]
Run Code Online (Sandbox Code Playgroud)
会产生你似乎从无效的宏语法中得到的效果.