gpt4 book ai didi

c - 二进制 "|"的无效操作数

转载 作者:太空宇宙 更新时间:2023-11-04 04:54:55 29 4
gpt4 key购买 nike

当我阅读有关 list.h 文件中的 hlist 的 FreeBSD 源代码时,我对这个宏感到困惑:

#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)

我正在尝试在我的函数中使用类似的样式,如下所示:

int *a;
int *b;
int *c;

if(a ? (b = (int *)0x0c) | (c = (int *)malloc(sizeof(int)) : NULL){

printf("test\n");
}

当我使用 GCC 编译它时,编译器给我错误,说:

invalid operands to binary "|"

我这样写我的函数,因为我认为“for”循环的条件返回“true”或“false”,但实际上编译器似乎告诉我它们不一样。然后我做不明白“for”和“if”之间的条件。

PS:我的母语不是英语,可能你听不懂,很抱歉。

最佳答案

你不能用指针|。您必须使用整数类型。我不知道你引用的 FreeBSD 代码,但他们可能使用 uintptr_t

无论如何,littleadv 是正确的。永远不要这样做。它是不可读且无法维护的官方文档。

关于c - 二进制 "|"的无效操作数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9952854/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com