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

时间:2014-12-09 22:58:15

标签: c pointers pass-by-value

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

以下是代码(根据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");
    }
}

2 个答案:

答案 0 :(得分:0)

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");
}

答案 1 :(得分:0)

在用户的大力帮助下(共享一些非常有用的链接),我已经设法解决了这个问题。

解决方案:修改调用者内存,将指针传递给该内存。

稍微更新后,我在这里正确地使用代码:

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

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

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


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(head);
    return 0;
}




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

    if ( (node = malloc (sizeof(struct DoubleList))) != NULL ){
        node->id=value;
        if ( (*headRef)==NULL) {
            // points to itself
            node->next=node;
            node->prev=node;
            (*headRef)=node;
        } else {
            struct DoubleList *current=(*headRef);
            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** headRef)
{
    if ( (*headRef)==NULL)
        printf("\nList is empty.\n\n");
    else {
        struct DoubleList *current=(*headRef);
        printf("\nThe list: ");
        do {
            printf("%x", current->id);
            current=current->next;
            if(current != (*headRef))
                printf("<->");
        } while(current!=(*headRef));
        printf("\n\n");
    }
}