Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/69.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 我无法通过引用传递我的节点_C_Pointers_Pass By Reference_C11 - Fatal编程技术网

C 我无法通过引用传递我的节点

C 我无法通过引用传递我的节点,c,pointers,pass-by-reference,c11,C,Pointers,Pass By Reference,C11,我不想创建general*head节点,我想通过引用和机会传递我的数据,但尽管为下一个节点创建新节点,但我无法到达main上的新节点。 如果我看n1,接下来我主要看到它是空的。为什么?怎么了 #include <stdio.h> #include <string.h> #include <stdlib.h> struct node{ int data; struct node* next; }; void add(struct node**

我不想创建general*head节点,我想通过引用和机会传递我的数据,但尽管为下一个节点创建新节点,但我无法到达main上的新节点。 如果我看n1,接下来我主要看到它是空的。为什么?怎么了

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

struct node{
    int data;
    struct node* next;
};

void add(struct node** head,int data){
    struct node * tmp = *head;

    while(tmp != NULL){
        tmp = tmp->next;
    }
    tmp = (struct node*) malloc(sizeof(struct node));
    tmp->data= data;
    tmp->next=NULL;
}

int main()
{
    struct node n1;

    n1.data=5;
    n1.next=NULL;

    add(&(n1.next),15);
    printf("%d",n1.next->data);

    return 0;
}
#包括
#包括
#包括
结构节点{
int数据;
结构节点*下一步;
};
void add(结构节点**head,int数据){
结构节点*tmp=*头;
while(tmp!=NULL){
tmp=tmp->next;
}
tmp=(结构节点*)malloc(sizeof(结构节点));
tmp->data=数据;
tmp->next=NULL;
}
int main()
{
结构节点n1;
n1.数据=5;
n1.next=NULL;
增加(&(n1.next),15);
printf(“%d”,n1.下一步->数据);
返回0;
}

您是否尝试传递列表中最后一个
下一个
指针,然后更新该指针以指向新节点,而不是使用头指针?如果是这样,
add()
应该是

void add(struct node** head, int data) {
    struct node* p = malloc(sizeof(*p));
    p->data = data;
    p->next = NULL;

    *head = p;
}

请指出将非空指针指定给
*head
?我没有head指针,所以我尝试通过引用传递。我不想使用head指针。所有代码都在这里