The main difference between pointer and reference is that pointer is a variable that holds the address of another variable while reference is an alias to access an already existing variable.
C++ is an enhancement of the C language. It is also considered as a superset of C. It is a powerful language and helps to build various applications such as operating systems, device drivers, etc. while providing many features to implement programs. One such a feature is dynamic memory allocation. Pointers and references are two concepts related to dynamic memory allocation.
Key Areas Covered
1. What is Pointer
– Definition, Functionality
2. What is Reference
– Definition, Functionality
3. What is the Difference Between Pointer and Reference
– Comparison of Key Differences
What is Pointer
A pointer is a variable that stores the address of another variable. In other words, it stores the direct address of a memory location. The programmer has to declare a pointer before using it to store a memory address. For example, assume, int *ptr; it explains that ptr is a pointer that holds the address of an integer. The * mark designates a variable as a pointer.
There are several steps to follow when using pointers. First, the programmer should define a pointer variable. Next, he has to assign the address of a variable to a pointer. Finally, the programmer can access the value. A simple example is as follows.
The variable ‘x’ holds the value 50. The ptr is a pointer to an integer. In line 9, ptr is assigned with the address of variable x. Now ptr points to x. Printing the value pointed by ptr gives the value of x which is 50.
What is Reference
A reference is an alias for an already existing variable. After the reference is initialized with a variable, it is possible to use the variable name or the reference name to refer that variable. Usually, a variable name is a label that helps to identify that variable’s memory location. A reference can be considered as a second label attached to that memory location. Therefore, the programmer can access the content of the variable by using the variable name or by using the reference.
int x = 50;
The reference variable for x is as follows.
int& r = x;
The variable x can store integer data type while r is an integer reference initialized to x. Then x is assigned with value 50. Line 10 prints the value of x by using the original variable name x while line 11 prints the value of x by using the reference r.
Difference Between Pointer and Reference
A pointer is a programming language object that stores the memory address of another value located in computer memory while a reference is an alias or another name for an already existing variable. This is the main difference between pointer and reference.
A pointer holds the address of a memory location while a reference is an alias for a variable that already exists.
The operator for pointer is * while the operator for reference is &.
There can be null pointers, but there cannot be null references. This is another difference between pointer and reference.
It is possible to initialize pointers at any time; however, a reference must be initialized when creating it.
Modification is another difference between pointer and reference. Pointers can point to another object at any time. However, after initializing a reference to an object, it is not possible to change it to refer to some other object.
Pointers and references are two concepts related to dynamic memory allocation in C++. The main difference between pointer and reference is that pointer is a variable that holds the address of another variable while reference is an alias to access an already existing variable.
1. “C Pointers.” Python Strings (With Examples), Available here.
Leave a Reply