karma( 업 )/C_C++

*& reference to pointer

생짜 2018. 12. 3. 13:30

간단하게 말해 pointer형 변수를 참조하겠다는 뜻이다.

아래 코드를 참조하면 이해가 될 것이다.


int a = 5;

int* b = &a;

printf("%d, %d", a, *b); //5, 5로 출력

int*& c = b; //포인터 변수 b를 참조

printf("%d, %d, %d", a, *b, c); //5, 5, 5로 출력

c=10;

printf("%d, %d, %d", a, *b, c); //10, 10, 10로 출력

int d = 3;

b = &d;

printf('%d, %d, %d", a, *b, c); //10, 3, 3로 출력 


 char *str = "Hello";


char *ptr = str;
char *&rptr = str;
char *world = "World";
ptr = world;

printf(str); // Hello 출력


rptr = world;

printf(str); //World 출력