C, C++는 pointers
를 지원한다. 이는 대부분의 다른 프로그래밍 언어(e.g Java, Python, Ruby.. etc)등 과 차별되는 점이며, 이들은 references
라는 선언을 지원한다.
재미있는 점은 C와 다르게 C++의 경우 pointers
뿐만 아니라, references
문법(혹은 선언) 또한 지원한다는 것이다.
오늘은 이러한 흥미로운 특징을 바탕으로 pointers
와 references
의 차이점에 대해 알아보도록 하자.
표면적으로는, references
와 pointers
둘 모두, 다른 객체에 접근(연결) 가능한 “값(즉, 다른 객체의 주소나 참조)”을 할당 받는다는 점에서 유사해 보인다.
이렇듯 두 선언 모두 유사한(혹은 동일해보이는) 기능을 지원하는 까닭에 그 차이를 명확히 알지 못하는 경우가 많다.
오늘은 이렇듯 유사한 기능으로써 동작하는 두 선언(문법)의 차이점을 알아도록 하자.
먼저 두 문법의 정의부터 정리하고 넘어가도록 하자.
Pointers: A pointer is a variable that holds the memory address of another variable. A pointer needs to be dereferenced with the ***** operator to access the memory location it points to.
References: A reference variable is an alias, that is, another name for an already existing variable. A reference, like a pointer, is also implemented by storing the address of an object.
출처: ‣
포인터 (Pointer):
포인터는 다른 변수의 메모리 주소를 저장하는 변수입니다.
포인터가 가리키는 메모리 위치에 접근하려면 *
연산자를 사용하여 역참조해야 합니다.