The majority of other programming languages, including Java, Python, Ruby, Perl, and PHP, only support references; C and C++, however, also support pointers. But strangely, C++ also offers references in addition to pointers.
On the surface, references and pointers appear to be extremely similar because both are employed to allow one variable to provide access to another. It's often difficult to distinguish what makes these methods different because they both offer many of the same features. In this tutorial, let us understand the major differences between references as well as the pointers in detail.
There are various distinctions between C++ references and pointers, despite the fact that they appear to be equivalent. A pointer is a variable that stores the address of another variable, whereas a reference is a variable that provides another name for an existing variable.
Pointers | References |
---|---|
|
|
For example
int i = 5;
// A pointer to the variable i or it stores the address of i
int *ptr = &i;
// A reference or alias for i.
int &ref = i;
Reference: An existing variable is also known as a reference variable. It is mostly used in "pass by reference" situations, where the reference variable is supplied to the function as a parameter and the function that receives will operate on the original copy of the variable.
Pointer: A pointer is a variable that keeps the location of another variable's address. It simplifies programming mainly by storing the memory address of a variable.
Reference: By using the symbol "&" before a variable, we will be able to declare a reference variable. If you use this symbol in your expression, then it will be considered an address operator.
Pointer: We must first declare a pointer variable before utilizing it, and this variable is made by adding the '*' operator just before a variable.
Reference: We are unable to reassign the reference variable. Now let's look at a brief example, which is shown below:
Multiple declarations of int &a are not permitted, as the code above shows. Since it is clearly understood that the reassignment operation is invalid for the reference variable.
#include <iostream>
using namespace std;
void func(int &);
int main()
{
int i; // variable declaration
int k; // variable declaration
int &a=i;
int &a=k; // error
return 0;
}
Pointer: It is possible to reassign the pointers. When working with data structures like linked lists, trees, etc., this reassignment is very helpful.
References: When using a reference, the address will be the same for the reference and the actual variable. Until the reference variable is either destroyed or leaves the scope, the new variable won't be associated with it.
For Example
#include <iostream>
using namespace std;
void func(int &);
int main()
{
int i;
int &a=i;
std::cout << " address of 'a' variable is : " <<&a<< std::endl;
std::cout << " address of 'i' variable is : " <<&i<< std::endl;
return 0;
}
Output:
address of 'a' variable is : 0x7ffdfb1460dc address of 'i' variable is : 0x7ffdfb1460dc
The output which is mentioned previously demonstrates that the reference variable's address and the real variable's address are identical.
Pointers: When using pointers, the memory locations of the actual variable and the pointer variable will differ. Let's look at an example to see how this works.
For Example,
#include <iostream>
using namespace std;
int main()
{
int c;
int *b;
b=&c;
cout<<"memory address of the p variable is :"<<&b;
cout<<"\nmemory address of the k variable is :"<<&c;
return 0;
}
Output:
memory address of the p variable is :0x7fff1ac4ec28 memory address of the k variable is :0x7fff1ac4ec24
The reference variable cannot be assigned to the NULL value, although the pointer variable can.
Reference
Reference to reference is not feasible in the case of references. The C++ software will produce a compile-time error if we attempt to accomplish it.
For example,
#include <iostream>
using namespace std;
int main()
{
int a=19; // initialization of variable
int &p=a; // creating a reference variable for ?a? variable.
int &&q=p; // the reference to reference is not valid, it will throws an error.
return 0;
}
Output:
/8090589a-5a34-4425-bf6a-d553525c80bf.cpp:7:10: error: cannot bind 'int' lvalue to 'int&&' int &&q=p; // the reference to reference is not valid, it will throws an error.
Pointers: Pointers could have a pointer-to-pointer relationship that provide more than one level of indirection.
For example,
#include <iostream>
using namespace std;
int main()
{
int *p;
int a=19;
int **q;
p=&a;
q=&p;
std::cout << "value of q is : " <<*q<< std::endl;
return 0;
}
Output:
value of q is : 0x7fffd83b054c
In the preceding code, the pointer 'p' points to variable 'a,' while 'q' is a kind of double pointer referring to 'p.' As a result, the value of 'p' is the address of 'a' variable, and the value of 'q' variable is the address of 'p' variable.
Pointers: arithmetic operations may be applied to the pointers, which is known as "Pointer Arithmetic," but arithmetic operations cannot be applied to the references. Reference Arithmetic does not exist in C++; there is no such thing as it.
Reference: Using an example, let's learn about the references.
#include
using namespace std;
int main()
{
int value=90; // variable declaration
int &a=value; // assigning value to the reference
&a=&a+5 // arithmetic operation is not all possible withthe reference variable, it throws an error.
return 0;
}
Output:
ERROR
Due to the fact that references and arithmetic operations are not compatible, the code above will fail to compile.
IMPORTANT NOTE
Because references are implemented internally as pointers, the performance is identical. However, to help you select what to use when keep these things in mind:
Use of References
Use of pointers