[Solved] Error When using a Macro in C++ to validate an IP


If you really want to implement this as a macro then you need to remove the return from inside the macro. You can’t assign the “result” of your macro to a variable as it is not a single statement.

#include <cstdio>

#define IPV4_ZEROVAL_CHECK(ip, result) {int d1,d2,d3,d4; result = (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;
    IPV4_ZEROVAL_CHECK (ip, x);
    printf("Value of x=%d \n",x);
    return 0;
}

There’s no advantage to implementing this as a macro. Leave it as a function, if its small enough the compiler will inline it when compiled in release mode.

solved Error When using a Macro in C++ to validate an IP