Ravi Uday <ra******@gmail .com> writes:
Can some-one comment on below snippet ?
static int xxx_list_compar e (void *ptr1, void *ptr2) {
if (ptr1 && ptr2 && (ptr1 == ptr2)) {
return (0);
}
return (1);
}
Are bit operations and comparing operations allowed on void * ?
Any redundancy in the code.
No, bit operations on void* are not allowed. Fortunately, there are
no bit operations in the code you posted. Equality comparisons are
perfectly ok.
The function returns 0 if both arguments are non-null and are equal to
each other; otherwise it returns 1. If that's what you want, it
looks correct.
Personally, I'd prefer to make the comparisons explicit, and I never
use parentheses on a return statement (they make it look too much like
a function call):
if (ptr1 != NULL && ptr2 != NULL && ptr1 == ptr2) {
return 0;
}
else {
return 1;
}
or perhaps even this:
return ptr1 == NULL ||
ptr1 == NULL ||
ptr1 != ptr2;
--
Keith Thompson (The_Other_Keit h)
ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.