Reputation: 3
I am just wondering is there a more efficient way to check if any argument passed to a function contains a NULL pointer? So far this is what I have been using as an argument check. I want the function to return -1 if any input is NULL.
int Func(const char *one, const char *two){
if( !one || !two ){
return -1;
}
//rest of the function code
}
This is fine for a function that accepts two arguments; however, it looks messy if I have a function that receives three or more arguments. Is there a better way to do it?
Upvotes: 0
Views: 223
Reputation: 67564
You can write your macros for that. For example for 5 parameters:
#define EVAL(a, ...)!(a) || EVAL1(__VA_ARGS__)
#define EVAL1(a, ...)!(a) || EVAL2(__VA_ARGS__)
#define EVAL2(a, ...)!(a) || EVAL3(__VA_ARGS__)
#define EVAL3(a, ...)!(a) || EVAL4(__VA_ARGS__)
#define EVAL4(a, ...)!(a)
#define MYASSERT5(...) EVAL(__VA_ARGS__)
int myfunc(const char *p1, const char *p2, const char *p3, const char *p4, const char *p5)
{
if(MYASSERT5(p1,p2,p3,p4,p5)) return -1;
}
Upvotes: 1