未初始化的局部变量错误 [英] Uninitialized local variable error

查看:75
本文介绍了未初始化的局部变量错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是使我陷入困境的功能,但我实际上不明白为什么.
此函数假定要从给定的链表中删除所有奇数元素,并且还返回已删除的奇数元素的新链表的地址.

Here is the function which getting me into troubles and I actually can't understand why.
This function supposes to removes all the odd elements from a given linked-list and also returns an address of a new linked list of the odd elements removed.

typedef struct node {
    int data;
    struct node* next;
} Node;


Node* removeOddValues(Node **source)
{
    Node *curr = *source;
    Node *even;
    Node *even_curr;
    Node *odd;
    Node *odd_curr;
    Node *next;

    even->next=NULL;
    odd->next=NULL;
    even_curr = even;
    odd_curr = odd;

    while(curr)
    {
        next = curr->next;
        curr->next = NULL;

        if(curr->data % 2!=0)// odd//
            odd_curr = odd_curr->next = curr;//node add to last//
        else                 //even//
            even_curr = even_curr->next = curr;

        curr = next;
    }
    *source= even->next;//update source//

    return odd->next;  //the new list of odd elements removed//
}

当我尝试编译它时,出现以下错误:

When I try to compile it, I get the following error:

warning C4700: uninitialized local variable 'even' used
warning C4700: uninitialized local variable 'odd' used

推荐答案

两件事:

首先,您会收到警告(并且您的程序包含未定义的行为,并且可能会崩溃),因为您访问/取消引用了未初始化的变量:

First, you get warnings (and your program contains undefined behaviour and would probably crash) because you access/dereference uninitialised variables:

Node *even;
Node *odd;
even->next=NULL;  // even has not been initialised
odd->next=NULL;   // odd has not been initialised

第二,您的代码不会记住"新列表的根,即您管理 odd_curr even_curr ,它们分别指向相应列表的最后一个节点,但没有 odd_root even_root 之类的东西.

Second, your code does not "remember" the roots of the new lists, i.e. you manage odd_curr and even_curr, each pointing to the last node of the respective list, but you do not have something like odd_root and even_root.

以下代码显示了它如何工作.对于两个列表(奇数和偶数),在末尾附加一个节点同时又考虑一个根节点的逻辑是相同的,因此将其分解为一个单独的函数:

The following code shows how this could work. The logic for appending a node at the end while additionally considering a root node is the same for both lists, odd and even, and therefore factored out into a separate function:

void appendNode(Node **root, Node** lastNode, Node *curr) {
    if (!*root) {  // root not yet assigned?
        *root = curr;
        *lastNode = curr;
    } else {
        (*lastNode)->next = curr;  // append curr after lastNode
        *lastNode = curr;  // let curr become the lastNode
    }
    (*lastNode)->next = NULL; // terminate the list at lastNode
}

Node* removeOddValues(Node **source)
{
    Node *curr = *source;
    Node *evenRoot = NULL;
    Node *oddRoot = NULL;
    Node *evenLast = NULL;
    Node *oddLast = NULL;

    while(curr)
    {
       Node *next = curr->next;
       if(curr->data % 2!=0) {
          appendNode(&oddRoot, &oddLast, curr);
       }
       else {
          appendNode(&evenRoot, &evenLast, curr);
       }
       curr = next;
    }
    *source= evenRoot;
    return oddRoot;
}

这篇关于未初始化的局部变量错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆