c - 指向结构的指针在每次函数调用时变为 NULL

标签 c pointers pass-by-value

问题是每次函数 addNodePos 被调用时 head 指针都是 NULL(在调试器中看到),它只是创建一个节点列表,它指向自身,因为它是一个循环双向链表。它显示 “List is empty.” 因为 list 在传递给 printList 函数时也是 NULL。一直在试图理解为什么,但仍然没有结果。

这是代码(根据 SSCCE 删除了过多的代码)

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

struct DoubleList
{
    int id;
    struct DoubleList *next;
    struct DoubleList *prev;
};

void addNodePos(struct DoubleList* head, int value, int position);
void printList (struct DoubleList* head);
//void clearList (struct DoubleList* head);


int main()
{
    int value, position;
    struct DoubleList *list = NULL;

    printf("\nvalue: ");
    scanf("%x", &value);
    printf("position: ");
    scanf("%d", &position);
    addNodePos(list, value, position);

    printf("\nvalue: ");
    scanf("%x", &value);
    printf("position: ");
    scanf("%d", &position);
    addNodePos(list, value, position);


    printList(list);
    //clearList(list);
    return 0;
}


void addNodePos(struct DoubleList* head, int value, int position)
{
    int i;
    struct DoubleList *node;

    if ( (node = malloc (sizeof(struct DoubleList))) != NULL ){
        node->id=value;
        if (head==NULL) {
            // points to itself as it is the only node in a list
            node->next=node;
            node->prev=node;
            head=node;
        } else {
            struct DoubleList *current=head;
            for (i = position; i > 1; i--)
                current=current->next;
            // reassign pointers -- relink nodes
            current->prev->next=node;
            node->prev=current->prev;
            node->next=current;
            current->prev=node;
        }
    }

    printf("Element has been added.\n\n");
}


void printList(struct DoubleList* head)
{
    if (head==NULL)
        printf("\nList is empty.\n\n");
    else {
        struct DoubleList *current=head;
        printf("\nThe list: ");
        do {
            printf("%d", current->id);
            current=current->next;
            if(current != head)
                printf("<->");
        } while(current!=head);
        printf("\n\n");
    }
}

最佳答案

head的地址是按值传递的,所以你的改动只体现在函数本身。您必须传递一个指向 head 地址的指针,以便您可以更改该值。

int main() { 
   ...
   addNodePos(&list, value, position);
   ...
}

void addNodePos(struct DoubleList** headPtr, int value, int position)
{
    struct DoubleList *head = *headPtr;
    int i;
    struct DoubleList *node;

    if ( (node = malloc (sizeof(struct DoubleList))) != NULL ){
        node->id=value;
        if (head==NULL) {
            // points to itself as it is the only node in a list
            node->next=node;
            node->prev=node;
            head=node;
        } else {
            struct DoubleList *current=head;
            for (i = position; i > 1; i--)
                current=current->next;
            // reassign pointers -- relink nodes
            current->prev->next=node;
            node->prev=current->prev;
            node->next=current;
            current->prev=node;
        }
    }

    printf("Element has been added.\n\n");
}

关于c - 指向结构的指针在每次函数调用时变为 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27390458/

相关文章:

C - 使用 goto 循环/导航

c - 您如何在函数内正确分配结构?

Java 对象属性的值传递解决方法

将字符串数组中的每个单词大写

python - 为什么这段代码选择乘以(1/16777216)而不是除以16777216?

c - 有一个用全局变量编写的已完成项目,我应该替换为通过引用传递吗?

c++ - 如何在C++中打印字符数组

c++ - 从基类实例获取指向(纯)虚函数的指针

c - 通过尾指针添加到链表,无需 3 级间接

c# - 按值(value)传递?