C中未使用的变量

0 c pointers linked-list

我正在编写C中的LinkedList实现.我遇到了以下问题: variable 'currentNode' set but not used.

我真的不明白这一点.我正在使用currentNode变量!

这是我的代码:

#include <stdio.h>
#include <stdlib.h>

struct node {
    int val;
    struct node * next;
};

int main()
{
    struct node root;
    root.val = 0;
    root.next = NULL;

    struct node currentNode;
    currentNode = root;

    int i;
    for (i = 0; i < 10; ++i)
    {
        struct node newNode;
        newNode.val = i;
        currentNode.next = &newNode;

        currentNode = newNode;
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

Ker*_* SB 5

你永远currentNode变量.如果删除了提及变量的所有行,则程序将完全相同.

(这确实是一个有用的警告:在你的情况下,可能是为了建立一个包含十个元素的列表,它指向代码中的致命错误,这意味着实际代码什么都不做.)