我正在练习链接列表代码。下面是该插件功能:传递引用时原始值不会改变?
Node* insert_at_pos(Node *head, int pos){
struct Node *ptr=NULL;
printf("enter data\n");
ptr=(Node*) malloc(sizeof(Node));
scanf("%d",&ptr->data);
ptr->next=NULL;
if (pos==0){
if (head==NULL){
head=ptr;
return head; //return that I want to remove
}
}
printf("done\n");
}
,而不是返回Node*
,如果我回到void
的,我觉得这个代码应该仍然工作,因为我参照传递价值。因此,head
的值应该自动更新而不是返回它,但如果我删除Node*
并将void
放在返回类型insert_at_pos
中,则该值不起作用。
而且,我打电话insert_at_pos
功能这样的:
Node *head=insert_at_pos(head,0);
可能是什么可能的解释或者是怎么回事错在这里?
可能的重复[如何修改已传递到C函数中的指针?](http://stackoverflow.com/questions/766893/how-do-i-modify-a-pointer - 它已被传递到函数在C) –
是的,它绝对是[如何修改已传递到C中的函数的指针](http:// stackoverflow .com/questions/766893/how-do-i-modify-a-pointer-that-has-been-passed-into-a-function-in-c) –