在链表追加指针的指针 [英] pointer of a pointer in linked list append
问题描述
我通常程序蟒蛇。为了提高我的模拟性能,我在学习C.我有一个问题,实施追加功能一个链表时,了解使用指针的指针。这是我的书code的摘录(由Kanetkar用C理解指针)。
I normally program in python. To increase performance of my simulations, I am learning C. I have a problem to understand the use of a pointer of a pointer when implementing the append function to a linked list. This is an excerpt of the code from my book (Understanding Pointers in C by Kanetkar).
#include <stdlib.h>
#include <stdio.h>
struct node{
int data;
struct node *link;
};
int main(){
struct node *p; //pointer to node structure
p = NULL; //linked list is empty
append( &p,1);
return 0;
}
append( struct node **q, int num){
struct node *temp, *r; //two pointers to struct node
temp = *q;
if(*q == NULL){
temp = malloc(sizeof(struct node));
temp -> data = num;
temp -> link = NULL;
*q = temp;
}
else{
temp = *q;
while( temp -> link != NULL)
temp = temp -> link;
r = malloc(sizeof(struct node));
r -> data = num;
r -> link = NULL;
temp -> link = r;
}
}
在此code,我的双指针**问传递给追加功能。我得到,这是该地址的ADRESS,即在这种情况下,空的ADRESS。
In this code, I pass the double pointer **q to the append function. I get that this is the adress of the address, i.e. the adress of NULL in this case.
我只是不明白的的为什么的人做像这样。这岂不是有效的去除全部都在同一*运营商追加()函数简单地传递NULL的ADRESS(即,p代替&安培; P)?到append()函数
I just don't get why one does it like this. Would it not be valid to remove one * operator from everything in the append() function and simply pass the adress of NULL (i.e. p instead of &p) to the append() function?
我GOOGLE了这个问题。这些问题的答案不是太费解(因为我只是一个C初学者),或过于平淡。我很感激任何提示,意见或链接,我可以了解这个读了。
I have googled this question. The answers are either too hard to understand (since I'm just a C beginner) or too plain. I'm thankful for any hints, comments or links where I can read up about this.
推荐答案
当你传递的东西在C函数,其是否变量或指针,它的原件及复印件。
When you pass things to functions in C, whether its variables or pointers, it's a copy of the original.
简单的例子:
#include <stdio.h>
void change(char *in)
{
// in here is just a copy of the original pointer.
// In other words: It's a pointer pointing to "A" in our main case
in = "B";
// We made our local copy point to something else, but did _not_ change what the original pointer points to.
}
void really_change(char **in)
{
// We get a pointer-to-a-pointer copy. This one can give us the address to the original pointer.
// We now know where the original pointer is, we can make _that one_ point to something else.
*in = "B";
}
int main(int argc, char *argv[])
{
char *a = "A";
change(a);
printf("%s\n", a); /* Will print A */
really_change(&a);
printf("%s\n", a); /* Will print B */
return 0;
}
因此,第一个函数调用更改()
传递了一个指针的拷贝到某个地址。当我们这样做 =中的B
只改变的的的我们得到通过指针的副本。
So the first function call to change()
gets passed a copy of a pointer to an address. When we do in = "B"
we only change the copy of the pointer we got passed.
在第二个函数调用, really_change()
,我们想传递一个指针到一个指针的副本。该指针包含地址我们原来的指针,瞧,我们现在可以参考原来的指针,改变一下原来的指针应指向。
In the second function call, the really_change()
, we get passed a copy of a pointer-to-a-pointer. This pointer contains the address to our original pointer and voila, we can now reference the original pointer and change what the original pointer should point to.
希望这解释了它稍微:)
Hopefully it explains it somewhat more :)
这篇关于在链表追加指针的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!