我通常使用python编程。为了提高我的模拟性能,我正在学习C语言。在将链接列表的附加函数实现时,我理解使用指针指针的问题。这是我书中的代码摘录(Kanetkar在C中的理解指针)。链接列表中指针的指针附加
#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;
}
}
在这段代码中,我将双指针** q传递给append函数。我知道这是地址的地址,即在这种情况下是NULL的地址。
我只是没有得到为什么这样做是这样的。从append()函数中的所有内容中移除一个*运算符并将简单的NULL(即p而不是& p)传递给append()函数是否无效?
我已经使用了这个问题。答案要么太难理解(因为我只是一个C初学者)或太简单。我很感激任何提示,评论或链接,我可以阅读这些内容。
ahhhh!这真的很有帮助。谢谢! :-) – seb 2013-03-06 10:27:47