0

我有一个奇怪的问题。我有这段代码,但它不起作用。奇怪的是,在函数内部,列表发生了变化(printf 命令表明这一点),但是当调用此函数时,列表中不会添加任何内容(我的列表不是空的)。

void pushToList(node* list, int val) {
    node* newNode = (node*) malloc(sizeof(node));
    newNode->value=val;

    newNode->next = list;
    list = newNode;

    printf("here, list->value = %d \n", list->value);
    printf("here, list->next->value = %d \n", list->next->value);
}

// ----------------------------------    
//    if (list==NULL) {
//        newNode->next = NULL;
//        list = newNode;
//    } else {
//        newNode->next = list;
//        list = newNode;        
//    }            

例如,我在我的 main 函数中调用此函数,如下所示:

node* node1;
pushToList(node1, 1111);

这是我在单独的头文件中的结构和 typedef(我已包含在我的函数文件中):

#ifndef STACKELEMENT_H
#define STACKELEMENT_H

struct stackElement {
    int value;
    struct stackElement* next;
};

typedef struct stackElement node;
#endif  /* STACKELEMENT_H */

另一个奇怪的行为是我有以下用于附加项目的函数,并且此函数仅在我的列表不为空时才有效:

int appendtoList(node* head, int val) {

    node* current = head;
    node* newNode = (node*) malloc(sizeof (node));

         if(newNode == NULL){
            fprintf(stderr, "Unable to allocate memory for the new node\n");
            exit(-1);
        }

    newNode->value = val;
    newNode->next = NULL;

    while (current->next) {
        current = current->next;
    }
    current->next = newNode;

    //    if (head->next == NULL) {                
    //        head->next = newNode;
    //    } else {                
    //        while (current->next != NULL) {
    //            current = current->next;
    //        }
    //        current->next = newNode;
    //    }
    //
        return 0;
}
4

2 回答 2

1

在函数中使用 node**list 作为参数类型。

当你将一个指向函数的指针传递给 struct node *x 到 void max (struct node*p); 指针是按值传递的,如果你想真正操作 x 指向的内容,使用 struct node** 作为参数类型并将 &x 传递给函数。

同样的逻辑应该适用于您的问题。

于 2013-09-14T14:57:17.313 回答
0

The problem was with the return type, i.e. the scope of a variable which in this case is a pointer variable. mbratch also pointed out this, thank you very much, but actually before reading mbratch's comment, I suddenly remembered a point from a lecture note about "accessing an object outside of its lifetime" which I think is different from "call by value/call by reference" problem. Just some clarifications for people who may run into this problem and may get confused: since we are allocating memory for the struct newNode INSIDE the function pushToList (even though using dynamic memory allocation command), the memory assigned to this variable would be free/destroyed when the function ends and the control returns back to the callee function (in this case, main()). So you should set the return type of your function to node* (a pointer to a node struct) and in your function return the head. Like this:

node* pushToList(node* head, int val) {
    node* newNode = (node*) malloc(sizeof(node));
    newNode->value=val;
    newNode->next = head;
    head = newNode;     
    return head;
}

In appendToList function, in addition to this mistake, as mbracth pointed out, I was doing another mistake by checking head->next (although implicitly) rather than head itself (to see if it's NULL): if head is NULL, you can not access head->next. Indeed two answers marked as the correct answers in some other posts here on stackoverflow misleaded me to this mistake. Anyway, here is the correct way:

        if (head == NULL) {
            head = newNode;
        } else {                
            while (current->next != NULL) {
                current = current->next;
            }
            current->next = newNode;
        }
于 2013-09-14T14:56:53.057 回答