2017-10-24 23 views
1

내가 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

enter image description here 상기도 포인터 포인터의 메모리를 나타내는 도면. 첫 번째 포인터 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; 
}