聘我网

新概念招聘3.0

宏中的"#"作用

vote up0vote downstar
#define redisAssert(_e) #_e

这里#_e如何理解?

 

1 个答复

vote up0vote downcheck

宏中:

#: 字符串
##: 符号

所以

redisAssert(name);

展开后相当于

"name";

再看个例子:

#define DEV_FILE_NAME    "/dev/test_kft"  

#define OPEN_FILE(fd, n)    \  
{   \  
    fd = open(DEV_FILE_NAME #n,O_RDONLY); \  
    if(fd < 0)  \  
    { \  
       printf("Open device error\n");  \  
        return 0; \  
    }   \  
} 

OPEN_FILE(fd1, 1);展开后便成为:

{ fd1 = open("/dev/test_kft" "1",00); if(fd1 < 0) { printf("Open device error\n"); return 0; } };  

其中"/dev/test_kft" "1"隐式字符串连接,在预处理阶段会转化为"/dev/test_kft1"

如果要显式连接:

char part1[] = "/dev/test_kft";
char part2[] = "1";
char path[strlen(part1) + strlen(part2) + 1];
strcpy(path, part1);
strcat(path, part2);
链接

您的回答





不是您要找的问题? 浏览其他含有标签 的问题或者 自己问个.