Hi everyone, inside this article we will see about C++ References and Pointers.
In C++, both references and pointers are used to indirectly access a variable or object. However, there are some differences between the two.
A reference is essentially an alias for a variable. It provides an alternative name for an existing variable, and allows us to manipulate the variable indirectly. Once a reference is initialized to a variable, it cannot be re-bound to another variable.
A pointer, on the other hand, is a variable that stores the memory address of another variable. It allows us to indirectly access the value of a variable by manipulating its memory address. Unlike a reference, a pointer can be re-assigned to point to a different memory location.
- Call-By-Value
- Call-By-Reference with a Pointer Argument
- Call-By-Reference with a Reference Argument
Example #1
Here is an example program that demonstrates the use of both references and pointers in C++:
#include <iostream> using namespace std; // Pass-by-Value int square1(int n) { cout << "address of n1 in square1(): " << &n << "\n"; n *= n; return n; } // Pass-by-Reference with Pointer Arguments void square2(int *n) { cout << "address of n2 in square2(): " << n << "\n"; *n *= *n; } // Pass-by-Reference with Reference Arguments void square3(int &n) { cout << "address of n3 in square3(): " << &n << "\n"; n *= n; } void example() { // Call-by-Value int n1 = 8; cout << "address of n1 in main(): " << &n1 << "\n"; cout << "Square of n1: " << square1(n1) << "\n"; cout << "No change in n1: " << n1 << "\n"; // Call-by-Reference with Pointer Arguments int n2 = 8; cout << "address of n2 in main(): " << &n2 << "\n"; square2(&n2); cout << "Square of n2: " << n2 << "\n"; cout << "Change reflected in n2: " << n2 << "\n"; // Call-by-Reference with Reference Arguments int n3 = 8; cout << "address of n3 in main(): " << &n3 << "\n"; square3(n3); cout << "Square of n3: " << n3 << "\n"; cout << "Change reflected in n3: " << n3 << "\n"; } // Driver program int main() { example(); }
Output
address of n1 in main(): 0x7fff1518902c
Square of n1: address of n1 in square1(): 0x7fff1518900c
64
No change in n1: 8
address of n2 in main(): 0x7fff15189030
address of n2 in square2(): 0x7fff15189030
Square of n2: 64
Change reflected in n2: 64
address of n3 in main(): 0x7fff15189034
address of n3 in square3(): 0x7fff15189034
Square of n3: 64
Change reflected in n3: 64
Example #2
Here is another example program that demonstrates the use of both references and pointers in C++:
#include <iostream> using namespace std; int main() { int value = 10; // An integer value int &ref = value; // A reference to an integer int *ptr = &value; // A pointer to an integer cout << "The value of the integer is " << value << endl; cout << "The value of the integer through the reference is " << ref << endl; cout << "The value of the integer through the pointer is " << *ptr << endl; value = 20; // Update the value of the integer cout << "The value of the integer is " << value << endl; cout << "The value of the integer through the reference is " << ref << endl; cout << "The value of the integer through the pointer is " << *ptr << endl; ref = 30; // Update the value of the integer through the reference cout << "The value of the integer is " << value << endl; cout << "The value of the integer through the reference is " << ref << endl; cout << "The value of the integer through the pointer is " << *ptr << endl; ptr = nullptr; // Set the pointer to null return 0; }
Output
The value of the integer is 10
The value of the integer through the reference is 10
The value of the integer through the pointer is 10
The value of the integer is 20
The value of the integer through the reference is 20
The value of the integer through the pointer is 20
The value of the integer is 30
The value of the integer through the reference is 30
The value of the integer through the pointer is 30
In this program, we first declare an integer variable value
, and then create a reference ref
that is initialized to value
, and a pointer ptr
that points to the memory location of value
.
We then print out the values of value
, ref
, and *ptr
(which is equivalent to the value of value
), and update the value of value
and ref
. We print out the values again, and observe that the values of value
, ref
, and *ptr
are all updated.
Finally, we set the value of ptr
to nullptr
(which is a null pointer), indicating that it no longer points to any memory location.
Note that in the program, the syntax for using references and pointers is different. To declare a reference, we use the &
symbol after the variable name, while to declare a pointer, we use the *
symbol before the variable name. To access the value of a variable through a reference, we simply use the reference variable name, while to access the value through a pointer, we use the *
symbol before the pointer variable.
We hope this article helped you to understand about C++ References and Pointers in a very detailed way.
Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.
If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.