为什么逻辑or会导致此`-Wint conversion`警告?

为什么逻辑or会导致此`-Wint conversion`警告?,c,C,星号线给了我以下警告: bool EnQueue(Queue *pq, Item item) { // 1. create a new node Node *node; if (QueueIsFull(pq)) return false; else { node = malloc(sizeof(Node)); if (node == NULL) { fprintf(stderr, &

星号线给了我以下警告:

bool EnQueue(Queue *pq, Item item) {
    
    // 1. create a new node
    Node *node;
    if (QueueIsFull(pq))
        return false;
    else {
        node = malloc(sizeof(Node));
        if (node == NULL) {
            fprintf(stderr, "Unable to allocate memory");
            exit(EXIT_FAILURE);
        }
    }
    
    // 2. Copy the item to the node, update next
    node->item = item;
    node->next = NULL;

    // 3. update the rear of the queue (and front if first object)
    pq->rear->next = node;
    pq->rear  = node;
    pq->front = pq->front || NULL; // ***********************

    // 4. update the queue size
    pq->items++;
    return true;
}


如果我删除
|
,并将其设置为一个或另一个值,则它工作正常,没有任何警告。为什么
| |
会发出警告?

如果两个参数都为0(或NULL),则逻辑OR运算符
|
的结果要么为0,要么为1。因此,该语句所做的是将
pq->front
设置为整数值0或1

您说希望结果是第一个非空值,类似于Python中
的工作方式。如果是这种情况,该语句基本上什么也不做,因为如果非null,它只会将
pq->front
赋值回自身,如果为null,则赋值回null

您可能正在寻找的是:

q.c:49:15: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
    pq->front = pq->front || NULL;
如果队列为空,则会将队列前端设置为
节点

另一方面,这是一个问题:

if (!pq->front) pq->front = node;
如果
pq->rear
为空。要解决这两个问题,请更改以下内容:

pq->rear->next = node;
为此:

pq->rear->next = node;
pq->rear  = node;
pq->front = pq->front || NULL;

当然,这假设如果
pq->front
pq->rear
为空,那么另一个也为空。

队列
定义为什么?
a | | b
的计算结果为1或0,然后尝试将该int赋值给指针。您希望该行做什么?@dbush--我认为它将计算为第一个非false对象。您需要类似于
if(!pq->front)pq->front=node如果你想做评论中说你想做的事。(在尝试取消引用之前,您需要确保
pq->rear
不为空)
if (!pq->front) {
    pq->front = node;
    pq->rear = node;
} else {
    pq->rear->next = node;
    pq->rear = node;
}