[英]I am trying to insert a new starting/head node in a linked list...The commented out function doesn't work while the one below it does the job
// void insert_start(struct node *head, int data)
// {
// struct node *ptr = (struct node *)malloc(sizeof(struct node));
// ptr->next_ptr = head;
// ptr->data = data;
// head=ptr;
// }
The above function does not work while the one below works上面的 function 不起作用,而下面的起作用
struct node *insert_start(struct node *head, int data)
{
struct node *ptr = (struct node *)malloc(sizeof(struct node));
ptr->next_ptr = head;
ptr->data = data;
return ptr;
}
see you problem is as follows, in the commented code, when you pass the pointer called head
to your function, another pointer pointing to the same address will be created, but you cannot change what does the original pointer in your main points to, it's like in the next picture:看到你的问题如下,在注释代码中,当你将名为
head
的指针传递给你的 function 时,将创建另一个指向相同地址的指针,但你不能改变你的 main 中的原始指针指向什么,它是如下图所示:
if you want to change what does your pointer in main function points to, then you have to pass a pointer to that pointer in order to change what does it points, to be something like the next picture:如果要更改主 function 中的指针指向的内容,则必须将指针传递给该指针以更改其指向的内容,如下图所示:
and in order to do that, you can modify your commented function as follow:为此,您可以修改评论的 function 如下:
void insert_start(struct node **head, int data)
{
struct node *ptr = (struct node *)malloc(sizeof(struct node));
ptr->next_ptr = *head;
ptr->data = data;
*head = ptr;
}
and in you main function when you call the function, pass to it the address of the pointer to change what does it point to like: insert_start(&head, data);
当您调用 function 时,在您的主要 function 中,将指针的地址传递给它以更改它指向的内容:
insert_start(&head, data);
The both functions declared like这两个函数都声明为
void insert_start(struct node *head, int data);
deal with a copy of the value of the pointer to the head node used as an argument expression.处理指向用作参数表达式的头节点的指针的值的副本。
So changing the copy within the functions does not influence on the value of the original pointer.因此,在函数中更改副本不会影响原始指针的值。 It stays unchanged.
它保持不变。
The only difference between the functions is that the second function returns to the caller the modified value of the copy of the original pointer to the head node.函数之间的唯一区别是第二个 function 将原始指针副本的修改后的值返回给调用者指向头节点。 So assigning the returned value from the function to the original pointer to the head node you can update its value.
因此,将 function 的返回值分配给指向头节点的原始指针,您可以更新其值。
Regarding your first (commented) example...关于您的第一个(评论)示例...
As mentioned in comments, C passes arguments by value.如评论中所述,C 按值传递 arguments。
In C, if the value of a variable is to be changed, then the address of that variable must be passed as opposed to the variable itself.在 C 中,如果要更改变量的值,则必须传递该变量的地址,而不是变量本身。
So in your original function:所以在你原来的 function 中:
void insert_start(struct node *head, int data)
{
struct node *ptr = (struct node *)malloc(sizeof(struct node));
ptr->next_ptr = head;
ptr->data = data;
head=ptr;
}
*head
contains the value of the address of an instance of struct node
. *head
包含struct node
实例的地址值。 As such, head
will not be changed upon return.因此,返回时
head
不会改变。
Should you want to use a form of the void
function that will modify the argument to allow it to change the address contained in *head
, then you must pass it's address: **head
.如果您想使用
void
function 的形式来修改参数以允许它更改包含在*head
中的地址,那么您必须传递它的地址: **head
。 Then in the body of the function, make the following changes.然后在 function 的主体中,进行如下修改。 (note reason cast has been removed .)
(注意原因演员已被删除。)
void insert_start(struct node **head, int data)
{
struct node *ptr = malloc(sizeof(*ptr));
if(ptr)//verify return of malloc before using
{
ptr->data = data;
ptr->next_ptr = (*head);
(*head) = ptr;//setting head to address of new node
}
}
Calling example: (called in a function such as main()
)调用示例:(在 function 中调用,例如
main()
)
struct node *new;
insert_start(&new, 10);//passing address of *new
if(!new)
{
//handle error
}
....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.