Key Difference – Pointer vs Reference
Before discussing the difference between pointer and reference, let us first look at the meaning of these two terms briefly. In a program, data occupy memory. The exact location of memory in which the data was placed in at an execution time should be known in order to access them. Pointer variables and reference variables are used to access these data and manipulate the memory addresses they are at. That is, both pointers and references can be used to refer to objects indirectly. They essentially hold memory addresses as their values. The key difference between a pointer and a reference is that a pointer is a variable which stores the address of the memory location of another variable while a reference is a variable that refers to another variable. The two mechanisms, the pointer and reference, have different syntax and usage.
What is a Pointer?
A pointer is a programming language object that stores the memory address of a value in the computing memory.
In C++ syntax;
int i = 5;
int *ptr = &i;
The first line defines a variable initialized with a value of 5. The second line defines a pointer to the variable’s memory address. Here, the ‘ptr’ is declared as an object of type ‘pointer to int’ whose initial value is the address of object i. Essentially ptr variable points to i by storing its memory address.
Obtaining the value stored at a memory location is called dereferencing. The * operator is used in order to dereference the pointer. After the declarations are done, the indirect expression *ptr dereferences ptr to refer to i.
To change the value of i, following assignment can be used;
*ptr = 20;
Pointers may be reassigned to refer to different objects as well.
A null pointer is a special type of pointer. A null pointer is often denoted by 0 or null and points to nothing. It’s good practice to use a null pointer in the case of not having an exact address to be assigned to the pointer. This assignment can be used in logical operations and conditions.
Pointers can be used to store and manage the addresses of dynamically allocated memory. Implementation and controlling of data structures such as stacks, queues, and lists can be made efficient with the use of pointers. For example, a queue may have two pointers; head pointer and tail pointer. Pointers can also be used to pass variables by their address, which would allow the value to be changed. Directly manipulating memory or memory-mapped devices can also be handled by using pointers.
Pointers allow both protected and unprotected access to memory addressed. Therefore, necessary precautions must be taken to verify that the content of a pointer is valid. Otherwise, dereferencing a pointer with an invalid memory address might cause the program to crash.
Pointers are supported in languages such as C, C++, Pascal and many assembly languages.
What is a Reference?
A reference is simply an alternative identifier for a value stored in the memory. It enables a program to indirectly access a particular datum.
In C++ syntax;
int i = 5;
int &ref = i;
The first line defines a variable initialized with a value of 5. The second line declares ‘ref’ as an object of type ‘reference to int’ which refers to i. A reference is different from the data itself given that a reference is implemented as the physical address of the particular datum that is stored in memory.
Once defined, a reference cannot be reassigned a new value. A reference always refers to the object with which it is initialized. In the following assignment, ref still refers to i, but the value is now 20.
ref = 20;
According to the above example, dereferencing references does not require any operators.
References can be used when a variable always refer to an object. This is because, unlike pointers, references cannot be null and must always be assigned at initialization. Since references must always refer to some object, making such a variable a reference ensures the program would work efficiently and correctly. Given that, there are no null references, the validity of a reference variable does not have to be tested.
Reference variables can be used to efficiently pass large data as arguments to procedures. References are also used in sharing large data between different code areas since each code keeps a reference to the data.
Programming languages such as C++, Java, Python, Perl, PHP, Ruby, etc. support references.
What is the difference between Pointer and Reference?
Definition of Pointer and Reference
Pointer: A pointer is the memory address of an object stored in computing memory.
Reference: A reference is an alternative identifier or an alias for an object.
Characteristics of Pointer and Reference
Pointer: A pointer is declared with the * operator.
Reference: A reference is declared with the & operator.
Pointer: A pointer variable requires the * operator to be dereferenced.
Reference: A reference variable requires no operator to be dereferenced.
Pointer: Pointers can be initialized to null. Such variables are called null pointers.
Reference: References cannot be initialized to null. There is no such thing as a null reference. A reference must always refer to an object.
Pointer: A pointer variable may be reassigned to refer to different objects.
Reference: A reference variable cannot be reassigned. It always refers to the object with which it was initialized.
Pointer: Pointer variables should be used when the possibility of referring to nothing exists or when it is required to refer to different things at different times.
Reference: Reference variables should be used when there will always be an object to refer to and when it’s not required to use that reference variable to refer to anything else other than that object.
Application of Usage
Pointer: Pointer variables can be used to implement algorithms and data structures.
Reference: Reference variables can be used in function parameters, and return types define useful interfaces.
Pointer: Programming languages such as C, C++, Pascal and many assembly languages support pointers.
Reference: Programming languages such as C++, Java, Python, Perl, PHP, Ruby, etc. support references.
Image Courtesy: “Pointers” by This file was made by User:SvenTranslation.Own work.This vector image was created with Inkscape.(CC BY-SA 3.0) via Commons –