他用大家在函數內部宏,錯誤使用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