【发布时间】:2018-11-18 23:20:48
【问题描述】:
我不想创建通用 *head 节点,我想通过引用传递我的数据,但是虽然为下一个节点创建新节点,但我无法在主节点上到达我的新节点。 如果我看 n1.next 在 main 我看到它是空的。为什么?有什么问题?
#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;
}
【问题讨论】:
-
请指出将非空指针分配给
*head的行? -
我没有头指针,所以我尝试通过引用传递。我不想使用头指针。所有代码都在这里
标签: c pointers pass-by-reference c11