指向链表中指针的指针 [英] Pointer to pointer in linked list

查看:95
本文介绍了指向链表中指针的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能解释一下为什么这段代码给我一个空列表:

Can someone explain me why this code give me as result the empty list:

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


void begin(node *head);
void display_list(node *head);


int main(){

node *head;
int i;

head = NULL;

for(i=0;i<5;i++) {
    begin(head);
}
display_list(head);




return 0;
}

void begin(node *head){
node *new;
int value;
new = (node*) malloc(sizeof(node));
printf("Insert the element to add at the beginning of the list: ");
scanf("%d",&value);
new->data = value;
new->next = head;
head = new;
}

但是如果我使用指向指针的指针更改 begin() 函数,它会给我正确的列表?

But if i change the begin() function with the pointer to pointer it gives to me the right list?

void begin(node **head){
node *new;
int value;
new = (node*) malloc(sizeof(node));
printf("Insert the element to add at the beginning of the list: ");
scanf("%d",&value);
new->data = value;
new->next = *head;
*head = new;
}

你能否解释一下为什么当我将主节点头传递给函数开始时,我必须将它作为&head"传递?不再是头"

Can you also explain me why when i pass in the main the node head to the function begin i have to pass it as "&head"? and no more as "head"

推荐答案

在此代码片段的第一个程序中

In the first program in this code snippet

head = NULL;

for(i=0;i<5;i++) {
    begin(head);
}

指针head通过值传递给函数begin.即创建了 main 中声明的指针 head 的值的副本,并将其分配给与函数 begin 同名的参数

the pointer head is passed to the function begin by value. That is a copy of the value of the pointer head declared in main is created and is assigned to the parameter with the same name of the function begin

void begin(node *head);

因此,在函数中,参数head 最初保存了更改后的原始指针head 的副本.原始指针head,其值被分配给参数没有被改变.

So within the function it is the parameter head that holds initially a copy of the original pointer head that is changed. The original pointer head the value of which was assigned to the parameter is not being changed.

要更改在 main 中声明的原始指针头,您必须通过指向指针头的指针间接通过引用将其传递给函数,就像在第二个程序中所做的那样.

To change the original pointer head declared in main you have to pass it to the function by reference indirectly through a pointer to the pointer head as it is done in the second program.

所以函数应该声明为

void begin(node **head);

而且你必须通过指向它的指针间接传递指针头

And you have to pass the pointer head indirectly through a pointer to it

begin( &head );

在这种情况下,取消引用传递的指针,函数将直接访问 main 中声明的原始指针头,并且可以更改它(而不是它在第一个函数定义中发生的值的副本)

In this case dereferencing the passed pointer the function will get a direct access to the original pointer head declared in main and can change it (not a copy of its value as it takes place in the first function definition)

new->next = *head;
*head = new;

为了更清楚地说明这个简单的演示程序.

To make it more clear consider this simple demonstrative program.

#include <stdio.h>

typedef int T;

void f( T t )
{
    t = 10;
}

int main(void) 
{
    T t = 0;
    
    printf( "Before calling f t is equal to %d\n", t );
    
    f( t );
    
    printf( "After  calling f t is equal to %d\n", t );

    return 0;
}

它的输出是

Before calling f t is equal to 0
After  calling f t is equal to 0

由于函数 f 处理传递的参数值的副本,因此在 main 中声明的变量 t 的值没有改变.

As the function f deals with a copy of the value of the passed argument the value of the variable t declared in main was not changed.

所以你需要通过像

#include <stdio.h>

typedef int T;

void f( T *t )
{
    *t = 10;
}

int main(void) 
{
    T t = 0;
    
    printf( "Before calling f t is equal to %d\n", t );
    
    f( &t );
    
    printf( "After  calling f t is equal to %d\n", t );

    return 0;
}

现在程序输出是

Before calling f t is equal to 0
After  calling f t is equal to 10

在这些演示程序中,名称 T 用作类型 int 的别名,并且主要对象 t 具有这种类型.

In these demonstrative programs the name T is used as an alias for the type int and in main the object t has this type.

现在假设名称 T 是 int * 类型的别名.

Let's now assume that the name T is an alias for the type int *.

typedef int * T;

在这种情况下,以 main 中的声明为例

In this case a declaration in main as for example

T t = NULL;

表示变量t具有指针类型int *.也就是说它等价于

means that the variable t has the pointer type int *. That is it is equivalent to

int * t = NULL;

所以要将它传递给一个必须改变原始变量 t 的函数,我们需要像这样通过引用传递它

So to pass it to a function that must change the original variable t we need to pass it by reference like

f( &t );

这意味着相应的函数应具有如下声明的参数类型

that means that the corresponding function shall have the parameter type declared like

void f( T *t );

但由于 Tint * 的别名,因此这意味着该函数具有 int ** 类型的参数.

but as T is an alias for int * hence it means that the function has a parameter of the type int **.

void f( int * *t );

这篇关于指向链表中指针的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆