我有一个节点结构的链表,在我的函数中搜索列表以查找具有匹配 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/