Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C中未使用的变量_C_Pointers_Linked List - Fatal编程技术网

C中未使用的变量

C中未使用的变量,c,pointers,linked-list,C,Pointers,Linked List,我目前正在用C编写LinkedList实现。我遇到了以下问题:变量'currentNode'集但未使用。 我真的不明白。我正在使用currentNode变量 这是我的代码: #include <stdio.h> #include <stdlib.h> struct node { int val; struct node * next; }; int main() { struct node root; root.val = 0;

我目前正在用C编写LinkedList实现。我遇到了以下问题:
变量'currentNode'集但未使用。

我真的不明白。我正在使用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;
}
#包括
#包括
结构节点{
int-val;
结构节点*下一步;
};
int main()
{
结构节点根;
root.val=0;
root.next=NULL;
结构节点currentNode;
currentNode=root;
int i;
对于(i=0;i<10;++i)
{
结构节点newNode;
newNode.val=i;
currentNode.next=&newNode;
currentNode=newNode;
}
返回0;
}
您永远不会读取
currentNode
变量。如果删除了所有提及变量的行,则程序将完全相同

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

您永远不会读取
currentNode
变量。如果删除了所有提及变量的行,则程序将完全相同


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

首先,您可能应该提到,只有在使用
-Wall
或特别是
-wUsed但设置变量时,才会出现此警告


其次,
gcc
用法的定义是从变量读取,而不是分配给变量。

首先,您可能应该提到,只有在使用
-Wall
或特别是
-Wunused但设置变量时才会出现此警告


其次,
gcc
用法的定义是从变量中读取,而不是分配给变量。

我发现了另一个问题

#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)
    {
        // newNode is a temporary value, 
        // its life time end before the next cycle 
        // u should new it 
        struct node newNode;
        newNode.val = i;
        currentNode.next = &newNode;

        currentNode = newNode;
    }

    // ...maybe like this 
    int i;
    for (i = 0; i < 10; ++i)
    {
        struct node* pNewNode = (struct node*)malloc(siof(struct node));
        pNewNode->val = i;
        currentNode.next = pNewNode;

        currentNode = *pNewNode;
    }

    // free node
    // ... 

    return 0;
}
#包括
#包括
结构节点{
int-val;
结构节点*下一步;
};
int main()
{
结构节点根;
root.val=0;
root.next=NULL;
结构节点currentNode;
currentNode=root;
// .... 
int i;
对于(i=0;i<10;++i)
{
//newNode是一个临时值,
//它的生命周期在下一个周期之前结束
//你应该把它换成新的
结构节点newNode;
newNode.val=i;
currentNode.next=&newNode;
currentNode=newNode;
}
//……也许是这样
int i;
对于(i=0;i<10;++i)
{
结构节点*pNewNode=(结构节点*)malloc(siof(结构节点));
pNewNode->val=i;
currentNode.next=pNewNode;
currentNode=*pNewNode;
}
//自由节点
// ... 
返回0;
}

我发现了另一个问题

#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)
    {
        // newNode is a temporary value, 
        // its life time end before the next cycle 
        // u should new it 
        struct node newNode;
        newNode.val = i;
        currentNode.next = &newNode;

        currentNode = newNode;
    }

    // ...maybe like this 
    int i;
    for (i = 0; i < 10; ++i)
    {
        struct node* pNewNode = (struct node*)malloc(siof(struct node));
        pNewNode->val = i;
        currentNode.next = pNewNode;

        currentNode = *pNewNode;
    }

    // free node
    // ... 

    return 0;
}
#包括
#包括
结构节点{
int-val;
结构节点*下一步;
};
int main()
{
结构节点根;
root.val=0;
root.next=NULL;
结构节点currentNode;
currentNode=root;
// .... 
int i;
对于(i=0;i<10;++i)
{
//newNode是一个临时值,
//它的生命周期在下一个周期之前结束
//你应该把它换成新的
结构节点newNode;
newNode.val=i;
currentNode.next=&newNode;
currentNode=newNode;
}
//……也许是这样
int i;
对于(i=0;i<10;++i)
{
结构节点*pNewNode=(结构节点*)malloc(siof(结构节点));
pNewNode->val=i;
currentNode.next=pNewNode;
currentNode=*pNewNode;
}
//自由节点
// ... 
返回0;
}

您正在尝试在堆栈上分配链表节点。这行不通;每个
newNode
都超出范围,并在每次
for
循环迭代后取消分配。您需要将节点改为
malloc
,并通过引用引用每个节点。@collonethirtytwo:如果OP从代码中删除
currentNode
,则可能会出现第二个警告,“
newNode
”。您正在尝试在堆栈上分配链表节点。这行不通;每个
newNode
都超出范围,并在每次
for
循环迭代后取消分配。您需要将节点改为
malloc
,并通过引用引用每个节点。@collonethirtytwo:如果OP从代码中删除了
currentNode
,希望会出现第二个警告,“
newNode
,但从未使用过”。