c - 整数比较失败

标签 c

我有一个节点结构的链表,在我的函数中搜索列表以查找具有匹配 id 的节点时,if 语句在比较他传入的 id 和节点 id 时似乎失败了。 if 语句位于下面函数的第 6 行。即使 *node_id* 和 id 具有相同的值,它也会失败。

NODE *node_id_search(int id, NODE *start) {
    NODE *result = NULL, *current = start;

    do {

        if(current->node_id == id) {
            result == current;
        }

        current = current->next;
    } while(current->next != NULL && result == NULL);


    return result;
}

节点.h

typedef struct node {
    /*@{*/
    /**
     * The node id used to identify the node. The id is a positive integer.
     */
    int node_id;

    /**
     * The node type.
     */
    char node_type[10];

    /**
     * Pointer to the next node element.
     */
    struct node *next;
    /*@}*/
} NODE;

最佳答案

除了上面提到的答案(我不知道它们与问题有何关系),我看到的唯一问题是这段代码:

    if(current->node_id == id) {
        result == current; //which should be result = current;
    }

将其更改为:

if(current->node_id == id){
     result = current;
     return result; // no need to search any further(hence optimized).
}
<小时/>

除此之外,我没有发现您的代码有任何问题。

关于c - 整数比较失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13552798/

相关文章:

c - long long数组在c中调用索引0时未返回期望值

c - 伪终端中的 Ncurses 程序

c++ - ‘int main;’ 是有效的 C/C++ 程序吗?

c - C 中不同类型的外部变量

c - 来自 UART 的数据用于更新数组

c - 将 2D 字符传递给函数并且仅填充第一个元素

c - 我的 C 语法存在许多多重选择错误

c - MS VS 2008 和 C99

C 有符号算术 : difference in representation between literals and variables

c - C 中的线程安全 char 字符串