【发布时间】:2018-08-23 13:29:34
【问题描述】:
我需要使用宏检查我的 ipaddr 是否等于 0.0.0.0。我编写了如下代码,但不断出现错误。
#define IPV4_ZEROVAL_CHECK(ip) {int d1,d2,d3,d4; return (4 == sscanf(ip, "%d.%d.%d.%d", &d1, &d2, &d3, &d4)) ? ((d1 == 0 && d2 == 0 && d3 == 0 && d4 ==0) ? 0 : 1) : 0;};
int main()
{
char ip[16] = "0.0.1.0";
int x = 99;
x= IPV4_ZEROVAL_CHECK (ipaddr);
printf("Value of x=%d \n",x);
return 0;
}
编译时出现如下错误
ipbox [root] # gcc test.cpp -lstdc++ -o test.o
test.cpp: In function 'int main()':
test.cpp:9: error: expected primary-expression before '{' token
test.cpp:9: error: expected `;' before '{' token
我错过了一些东西,但就是无法解决它。如果我写的是函数,它运行良好。我很好奇为什么它不能作为 MACRO 工作
【问题讨论】:
-
为什么使用 MACRO 而不是常规(内联)函数?
-
请不要为此使用宏
-
除了已经说过的内容之外,缺少 ipaddr 变量
-
那个宏应该是一个函数。太理清为什么它没有做你想做的事,首先将宏中的代码复制并粘贴到你的
main函数中。