+5  A: 

This seems to suppress the warning for me:

#define SAFE_CMP(a,b) ((void *)(a) != NULL && (void *)(b) != NULL) ? cmp(a,b) : 0

...but personally, I would just create safe_cmp() as a function itself.

int safe_cmp(struct foo *a, struct foo *b) {
    return (a && b) ? (a->bar == b->bar) : 0;
}
caf
cmp() in my actual working source is _macro_ also. I think it's better to write into inline function. Very thanks[
cwyang
A: 

"I'd like to have one helper macro appricable to all situation."

Why? One size does not fit all. GCC is doing you a favor by telling you a comparison will always have a certain result. The address of a stack variable will never be NULL. I would just write out the check in baz:

int baz(struct foo *a) {
   struct foo b;
   ...
   return a == NULL ? 0 : cmp(a, &b);
}

You could also do it in cmp. It depends how you define the pre and post-conditions.

Another possible issue with your macro (not applicable for baz) is that a and b will be evaluated multiple times. Beware of:

SAFE_CMP(p++, p1++);
Matthew Flaschen
I think It's a matter of taste. I'd personally favor all-in-one approach. I'll be happy if gcc checks null whenever I pass two dynamic pointer value. However when I pass static pointer value using that macro, I want gcc not to check and produce such an annoyuing warning.It's obvious that "return SAFE_CMP(a, " is more clear and readable than "return a == NULL ? 0 : cmp(a, "Anyway thanks for your kind reply. Thank you.
cwyang
A: 

The gcc option -Wno-address seems to remove the warnings.

Joseph Quinsey