Both pointers and references are the most powerful features in C and C++ which allow programmers to manipulate memory address directly for efficient memory management.
Both are variables that are used to refer to other objects indirectly and they share some common features on the surface. Though they share lots of common capabilities, they are also very complex to understand.
A pointer is a variable that holds the memory address of another variable, whereas a reference is like a pointer that can be used to indirectly refer to another variable.
Most programmers seem to develop some understanding about when to use pointers and when to use references, but they often come across problems the solution of which seems unclear.
So to better understand the philosophy, let’s understand the difference between the two.
What is a Pointer?
Every variable is a memory location which is defined by its address and is paired with an identifier which contains some value that may be changed during the course of a program. In simple terms, variable is a symbol that represents a quantity.
A pointer is a variable that contains the memory address of another variable. A pointer is nothing but an address and a variable that stores a pointer is called a pointer variable.
The idea is to use identifiers (or names) instead of numeric addresses to ease the burden of programming thereby reducing complexity of programs. Since these addresses refer to actual locations in the computer memory, pointers can be used to access and manipulate content stored in the memory.
Like any variable or constant, pointers must be declared for their types. Since pointers contain memory addresses that belong to a separate data type, they must be declared using pointers before you can work with them. The general form of a pointer declaration is:
Data_type * var-name;
Here, “Data_type” is the base type of the pointer and “var-name” is the pointer variable. The asterisk (*) here is used to declare the pointer which designates the variable “var-name” as a pointer variable.
int *p; // integer pointer
float *x; // float pointer
Here, the variable “p” points to an integer data type which is declared as “int” which refers to the data type of the variable. Similarly, in the second example, “float” refers to the data type of the variable “x” which in fact is designated as a pointer to a floating-point variable.
What is a Reference?
A reference is like a pointer with indirect access to a variable. It’s like an alias for another variable, meaning it’s just another name for an already existing variable.
It allows programmers to create a second name for the variable which they can use to manipulate the content stored in that variable. It’s an entity that is an alias for another variable and it has the same address as the original variable. It supports pass-by-reference acting as function formal parameters and once it’s defined to refer to a variable, it cannot be reseated which means it cannot be referred to another variable.
Unlike pointers, they don’t need dereferencing operator to access the value; in fact, they can be used like normal variables.
int &ri = i;
Here, “ri” is an integer reference initialized to “i”. It declares “ri” as an object of type “reference to int”. A reference can be easily confused with a pointer which is an independent variable, the address values of which cannot be reassigned once created.
Difference between Pointer and Reference
Basics of Pointer and Reference
– Both provide almost similar capabilities in terms of usability and the distinction can be tricky. A pointer is just a variable that stores the memory address of another variable, whereas a reference is like an alias for an already existing variable. It creates a second name for a variable that can be used to modify the content stored in that variable.
Declaration of Pointer and Reference
– A pointer is like any other variable which stores a piece of data. Unlike other variables, pointer variables store memory addresses instead of values. A pointer is dereferenced using the indirection operator * to access the memory location of the variable it points to. A reference, on the other hand, doesn’t need any dereferencing operator to access the value.
Reassignment of Pointer and Reference
– Unlike pointers which can be initialized at any time, references must be assigned at initialization. Pointers can be accessed directly, whereas references cannot be directly accessed.
Memory Address for Pointer and Reference
– A pointer variable has its own location in memory, which means when you declare a pointer, you specify a location which is a memory address assigned to each unit of memory. The pointer variable has the same address where it’s saved in memory. A reference variable, on the other hand, is an alias for another variable thereby taking the address of the reference variable.
NULL Value for Pointer and Reference
– NULL represents the end of the list which means it indicates the value is unknown and it specifies that variable does not have a valid address space. NULL value can be assigned to a pointer variable directly which suggests it does not point anywhere, whereas NULL cannot be assigned to a reference variable.
Pointer vs. Reference: Comparison Chart
Summary of Pointer vs. Reference
Pointers have always been among the favorite topics of discussion of C/C++ programmers until references came to the picture. References are somewhat similar to pointers but have their fair share of pros and cons in comparison. While both can be used to change local variables of one function within another function, they have their differences. Both are powerful features in C and C++ which allow you to directly manipulate memory, but with a few exceptions. A pointer holds the memory address of a variable, whereas a reference is just an alias for another variable.
- Difference Between Whole Genome Sequencing and Next Generation Sequencing - January 24, 2021
- Difference Between Biometric Authentication and Password - January 23, 2021
- Difference Between Neuroevolution and Deep Learning - January 23, 2021