[英]C Macro concatenation as function argument
I have a problem using a macro as a function parameter.我在使用宏作为函数参数时遇到问题。
I have this macro:我有这个宏:
#define PD13 GPIOD, GPIO_Pin_13
GPIOD
and GPIO_Pin_13
are macros too. GPIOD
和GPIO_Pin_13
也是宏。
#define GPIO_Pin_13 ((uint16_t)0x2000)
#define GPIOD ((GPIO_TypeDef *) GPIOD_BASE)
in which GPIOD_BASE
is a memory address.其中
GPIOD_BASE
是内存地址。
I'm using it as argument for this function:我将它用作此函数的参数:
PinType initPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin) {
PinType pin;
if (GPIOx == GPIOA) {
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
} else if (GPIOx == GPIOB) {
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
} else if (GPIOx == GPIOC) {
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
} else if (GPIOx == GPIOD) {
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
} else {
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
}
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOx, &GPIO_InitStructure);
GPIO_ResetBits(GPIOx, GPIO_Pin);
pin.GPIO_Reg = GPIOx;
pin.GPIO_Pin = GPIO_Pin;
pin.status = 1;
return pin;
}
PinType p1
p1 = initPin(PD13);
In this way everything works well, but if I try to add an argument to the initPin
function, like:通过这种方式一切正常,但是如果我尝试向
initPin
函数添加一个参数,例如:
void initPin(PinType *pin, GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin) {
if (GPIOx == GPIOA) {
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
} else if (GPIOx == GPIOB) {
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
} else if (GPIOx == GPIOC) {
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
} else if (GPIOx == GPIOD) {
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
} else {
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
}
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOx, &GPIO_InitStructure);
GPIO_ResetBits(GPIOx, GPIO_Pin);
pin->GPIO_Reg = GPIOx;
pin->GPIO_Pin = GPIO_Pin;
pin->status = 1;
}
PinType p1;
initPin(&p1, PD13);
The arguments are messed up and are incorrect.论点混乱且不正确。
Is there any way so I can use a macro for passing a part of the arguments while the others are passed normally?有什么办法可以使用宏来传递一部分参数,而其他参数则正常传递?
Is there any way so I can use a macro for passing a part of the arguments while the others are passed normally?
有什么办法可以使用宏来传递一部分参数,而其他参数则正常传递?
No. Macro expansion is entirely textual and there are no mechanisms to suppress expansion of selected arguments.不。宏扩展完全是文本形式,并且没有抑制所选参数扩展的机制。
You could, however, define different macros to achieve a similar effect:但是,您可以定义不同的宏来实现类似的效果:
#define PD13_2args GPIOD, GPIO_Pin_13
#define PD13_1arg GPIO_Pin_13
initPin(PD13_2args);
initPin(&p1, PD13_1arg);
You might also want to read about variadic macros , a feature available with C99 and later, in conjunction with variadic functions (eg like printf
).您可能还想了解可变参数宏,这是 C99 及更高版本提供的功能,与可变参数函数(例如
printf
)结合使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.