Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 指向结构的指针在每次函数调用时都变为NULL_C_Pointers_Pass By Value - Fatal编程技术网

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

C 指向结构的指针在每次函数调用时都变为NULL,c,pointers,pass-by-value,C,Pointers,Pass By Value,问题是,每次调用函数addNodePos时,头指针都是NULL,这在调试器中看到,它只创建一个节点列表,该列表指向自身,因为它是一个循环双链接列表。并显示列表为空。因为list在传递给printList函数时也是NULL。一直在试图理解为什么,但仍然没有结果 下面是根据删除的代码 head的地址是通过值传递的,因此您的更改只反映在函数本身中。您必须传递一个指向head地址的指针,以便更改该值 int main() { ... addNodePos(&list, value,

问题是,每次调用函数addNodePos时,头指针都是NULL,这在调试器中看到,它只创建一个节点列表,该列表指向自身,因为它是一个循环双链接列表。并显示列表为空。因为list在传递给printList函数时也是NULL。一直在试图理解为什么,但仍然没有结果

下面是根据删除的代码


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

在分享了一些非常有用的链接的用户的大力帮助下,我成功地解决了这个问题

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

经过一点更新后,我在这里留下了正确的工作代码:

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

看起来你也犯了同样的错误。一旦你修改了head,你就再也不会更新*headPtr了。你可以写&list而不是head。@MattMcNabb,这样代码看起来会更清晰一些。更新!
#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");
    }
}