C and C++ accept pointers in a way that most other programming languages do not. References are supported by other languages such as C++, Java, Python, Ruby, Perl, and PHP.
Both references and pointers seem to be very similar on the surface; both are used to allow one variable provide access to another. For both having much of the same capabilities, it is often unclear what distinguishes these two systems. In this post, we will discuss the difference between pointers and references.
Pointer vs Reference
1)Pointers
A variable that holds the memory address of another variable is known as a pointer. To access the memory location that a pointer points to, it must be dereferenced with the * operator.
2)Reference
A reference variable is an alias, or another name for a variable that already exists. A reference, like a pointer, is implemented by storing an object’s address.
A reference can be thought of as a constant pointer (not to be confused with a constant value pointer!). The compiler can apply the * operator for you if you use automatic indirection.
3)Differences
1)A reference is a type of const pointer that automatically de-references itself. Yes, it is similar to a const pointer in that once a reference is attached to a variable or entity, it cannot be changed to point to anyone else.
If you attempt to point an already initialised reference to another variable, it will not change its reference point instead, this type of assignment will only change the value of the variable to which the reference was pointing.
In contrast, you can adjust the value of a pointer i.e, force a pointer to point to a new memory location at run time.
Below is the implementation:
#include <iostream> using namespace std; int main() { int first_element = 50; int second_element = 100; // First, set Reference ref1 to point to a variable // first_element int& ref1 = first_element; cout << "first element = " << first_element << endl; cout << "second element = " << second_element << endl; cout << "reference 1 = " << ref1 << endl; /* If you attempt to point the reference ref 1 to another variable, say second_element, it will not change its reference point instead, this type of assignment will only change the value of the variable to which the reference was pointing.*/ ref1 = second_element; cout << "After modifying the reference 1 value , ref1 = " << ref1 << endl; cout << "first element = " << first_element << endl; cout << "second element = " << second_element << endl; // initializing a pointer that points to first value int* ptr = &first_element; cout << "pointer = " << ptr << " :: *ptr = " << (*ptr) << endl; // modifying the value of second element second_element = 200; ptr = &second_element; cout << "pointer = " << ptr << " :: *ptr = " << (*ptr) << endl; return 0; }
Output:
first element = 50 second element = 100 reference 1 = 50 After modifying the reference 1 value , ref1 = 100 first element = 100 second element = 100 pointer = 0x7ffc457fec50 :: *ptr = 100 pointer = 0x7ffc457fec54 :: *ptr = 200
2)It is needed to initialise a reference while declaring it, i.e. A pointer, on the other hand, may be declared without being initialised. As a result, pointer may also be set to NULL. In contrast, it is not possible with a Reference.
Example:
int value = 100; int& reference = value; // correct way to initialize int& var; // it is wrong and it won't compile int* pointer; // correct way pointer = NULL; // Correct way reference = NULL; // it is wrong and it won't compile
3)You cannot have a reference to a reference, but you can have a pointer to a pointer.
4)A pointer’s value can be incremented and decremented, and it can be used for random indexing, but this is not possible with a reference.
Example:
int* pointer = new int[100]; *(pointer + 5) = 100;
5)If you take a reference’s address, it will be identical to the address of the variable to which it is referring. In the case of a pointer, though, the situation is different.
6)If pointers are allocated on heap, there is always a chance and extra effort to delete them. There may be issues such as memory leaks and hanging pointers. In the case of references, however, it is fine, and you do not need to be concerned with such issues.
7)A pointer on the stack has its own memory address and size, while a reference on the stack has the same memory address (as the original variable) but takes up some stack space.
8)Pointers can perform a variety of arithmetic operations, but there is no such thing as Reference Arithmetic. (However, as in &obj + 12, you can take the address of an object pointed by a reference and perform pointer arithmetic on it.)
Related Programs:
- compare and get differences between two lists in python
- python program to generate a dictionary that contains numbers between 1 and n in the form x xx
- how to code a scraping bot with selenium and python
- how to create and initialize a list of lists in python
- python how to create a list and initialize with same values
- how to create a navigation bar and sidebar using react
- what is the difference between final and immutable in java