【问题标题】:pointer to a struct becomes NULL at every function call指向结构的指针在每次函数调用时都变为 NULL
【发布时间】:2015-02-08 00:35:44
【问题描述】:

问题是每次函数addNodePos被调用head指针是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");
    }
}

【问题讨论】:

标签: c pointers pass-by-value


【解决方案1】:

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

解决方案:要修改调用者内存,传递一个指向该内存的指针。

稍作更新后,我留下了正确工作的代码:

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

【讨论】:

  • 你可以写&amp;list而不是head
  • @MattMcNabb,很好的提及)这样代码看起来会更清晰一些。更新了!
【解决方案2】:

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

【讨论】:

  • 看起来你也犯了同样的错误。
  • 一旦你修改了*headPtr,你就再也不会更新head
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-24
  • 1970-01-01
  • 2018-01-21
相关资源
最近更新 更多