他用大家在函数内部宏,错误使用C
我想在C使用这个宏:
#define CONTROL_REG(num_device) CONTROL_DEV_##num_device
但它不工作。这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#define CONTROL_DEV_0 0x00 /* control register for device 0 */
#define CONTROL_DEV_1 0x01 /* control register for device 1 */
#define CONTROL_DEV_2 0x02 /* control register for device 2 */
#define CONTROL_DEV_3 0x03 /* control register for device 3 */
#define CONTROL_REG(num_device) CONTROL_DEV_##num_device
void func(int num_device)
{
printf("Value: %d\n", CONTROL_REG(num_device));
}
int main(int argc, char **argv)
{
func(0);
func(1);
func(2);
func(3);
}
任何建议吗?
此致敬礼。
宏是编译时,你试图在运行时使用它们。 – Art
好的,理解。非常感谢你。 – oscargomezf