While I am reading the FreeBSD source code about the hlist in the file list.h, I am confused about this macro:
#define hlist_for_each_entry_safe(tp, p, n, head, field) \
for (p = (head)->first; p ? \
(n = p->next) | (tp = hlist_entry(p, typeof(*tp), field)) : \
NULL; p = n)
and I am trying use the similar style in my function as below:
int *a;
int *b;
int *c;
if(a ? (b = (int *)0x0c) | (c = (int *)malloc(sizeof(int)) : NULL){
printf("test\n");
}
when I use GCC compile it, the compiler give me the error, says:
invalid operands to binary "|"
I write my function in that way, because I think the condition of the “for” circulation returns “true” or “false”, but in fact the compiler seems to tell me they are not the same.And then I do not understand the condition between “for” and “if”.
PS:My mother tongue is not English, maybe you could not understand it well, I am sorry about that.
You can’t
|with pointers. You have to use an integral type. I don’t know about the FreeBSD code you cite, but they may useuintptr_t.Regardless,
littleadvis right. Don’t ever do this. It is unreadable and unmaintainable gobbledygook.