我做了删除fct二叉树标题,它工作了!但是我没有做任何努力,我依赖于调试器。
你能解释一下如何在简单指针,双指针和值之间建立联系吗?
Tree **P2,*P1,P; //(P consider it for explanation only)
P1=&P2;
//what type are these
(*P2)->data;
&(*P2)->data;
P1->data;
*P1->data;
答案 0 :(得分:0)
上图显示了指针指针的内存表示。第一个指针ptr1(在你的情况下为P2
)存储第二个指针ptr2的地址,第二个指针ptr2(在你的情况下为P1
)存储变量的地址。
#include <stdio.h>
#include <stdlib.h>
typedef struct _s {
int data;
} Tree;
int main(int argc, char *args[])
{
/*
* Arrow operator example
* foo->bar is equivalent to (*foo).bar
* it gets the member called 'bar' from the struct that 'foo' points to.
*/
Tree **P2,*P1;
P1 = (Tree*)malloc(sizeof(Tree));
P1->data = 789;
//P1=&P2; // It's wrong, Incompatible pointer types Tree* and Tree***
P2 = &P1; // It's right, p2 points to address of p1
printf("%d\n", (*P2)->data); // same as (**P2).data
printf("%p\n", (void*) &(*P2)->data); // same as &(**P2).data, (void* casting to print address)
printf("%d\n", P1->data); // same as (*P1).data
//printf("%d",*P1->data); // same as *(P1->data), it's already dereferenced type, you're trying to dereference again??
free(P1);
return 0;
}