[자료구조] 포인터의 포인터의 의미와 사용방법

2021. 4. 25. 15:32
728x90

 

포인터의 포인터

 : 포인터 변수를 가리키는 포인터 변수

 

int int_value = 100;
int *ptr_int = &int_value;
int **pptr_int = &ptr_int;

포인터의 포인터는 주소가 저장된 포인트 변수의 주소를 저장하는 변수.

변수 int_value : 100

포인터 변수 ptr_int : c300

포인터 변수 ptr_int가 가리키는 변수의 값 : 100

더블포인터 변수 pptr_int : b200

더블포인터 변수 pptr_int 가 가리키는 주소 : c300

더블 포인터 변수 pptr_int 가 가리키는 주소가 가리키는 값 : 100

 

- 행과 열의 크기에 따라 동적으로 배열을 생성

int row = 3;
int col = 4;
int **pptr_int_array = NULL;

pptr_int_array =(int**)malloc(sizeof(int*)*row);
for(i = 0; i < row; i++){
   pptr_int_array[i] = (int)* malloc(sizeof(int)*col);
   memset(pptr_int_array[i],0,sizeof(int)*col);
}

 

2000-2003 1000 1000-1003 1004-1007 1008-100b 100c-100f
0 0 0 0
2004-2007 1010 1010-1013 1014-1017 1018-101b 101c-101f
0 0 0 0
2008-200b 1020 1020-1023 1024-1027 1028-102b 102c-102f
0 0 0 0

시작주소가 값이 되는 것, pptr_int_array는 200

 

pptr_int_array[0][0] = 1;
**pptr_int_array = 1;

pptr_int_array[2][3] = 1;
*(*pptr_int_array + 2) + 3) = 1;

**pptr_int_array = 1 은

pptr_int_array[0][0]을 1로 변경하라는 의미로 두 의미가 같은 의미이다.

 

 

pptr_int_array[2][3] = 1;

*(*(pptr_int_array +2) +3 ) = 1

2000-2003 1000 1000-1003 1004-1007 1008-100b 100c-100f
0 0 0 0
2004-2007 1010 1010-1013 1014-1017 1018-101b 101c-101f
0 0 0 0
2008-200b 1020 1020-1023 1024-1027 1028-102b 102c-102f
0 0 0 1

(pptr_int_array +2) = 2008

*(pptr_int_array +2) = 1020

*(pptr_int_array +2) +3 = 102c //여기서 3은 3칸, 3*4 12byte

*(*(pptr_int_array +2) +3 ) 에 1 을 저장.

 

구조체에 대한 포인터 변수

struct student student_lee;
struct student *ptr_student = NULL;

//주소연산자 이용하여 복사하기
ptr_student = &student_lee;

//구조체 복사하여 값 설정하기
strcpy(ptr_student -> name, "test");
ptr_student -> grade = 80.1f;
ptr_student -> num = 20041235486;

//포인터 이용
strcpy((*ptr_student).name,"test");
(*ptr_student).grade = 80.1f;
(*ptr_student).num = 20041235486;

 

 

반응형

BELATED ARTICLES

more