
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Passing by Pointer vs Passing by Reference in C++
In C++, If we want to pass arguments to functions we can either pass the actual value, a pointer to the value, or a reference to the value. This concept becomes crucial when we want a function to modify the original variable.
So, if we pass parameter to a function either by pass by pointer or pass by reference it will produce the same result. Only difference is that References are used to refer an existing variable in another name whereas pointers are used to store address of variable. It is safe to use reference because it cannot be NULL.
Let us understand Passing by Pointer vs Passing by Reference individually:
Passing by Pointer
In this method, the function receives the address of the variable and inside the function, we dereference the pointer to access or modify the value.
- Here, the function parameter uses an asterisk (*) to indicate it is a pointer.
- We use the address-of operator (&) to send the address of the variable when calling the function.
Syntax
Following is the syntax for passing by pointer:
void modify(int* ptr) { *ptr = 20; }
Example 1
Following is the basic example of modifying a variable using a pointer.
#include<iostream> using namespace std; void updateValue(int* x) { *x = *x + 10; } int main() { int num = 5; updateValue(&num); cout<<"Updated Value:"<<num<<endl; return 0; }
The above program produces the following result:
Updated Value: 15
Example 2
In this example, we are swapping values using pointer approach.
#include<iostream> using namespace std; void swap(int* a, int* b) { int temp = *a; *a = *b; *b = temp; } int main() { int x = 3, y = 7; swap(&x, &y); cout << "x: "<< x <<", y:"<< y<<endl; return 0; }
The above program produces the following result:
x: 7, y: 3
Passing by Reference
In this method, the function gets a direct reference to the original variable. Any change inside the function directly reflects in the original variable.
- The function parameter uses an ampersand (&) to define it as a reference.
- We just pass the variable name in the function call ? no need for & or *.
Syntax
Following is the syntax for passing by reference:
void modify(int& ref) { ref = 30; }
Example 1
Basic example of modifying a variable using reference.
#include <iostream> using namespace std; void updateValue(int& x) { x = x + 20; } int main(){ int num = 10; updateValue(num); cout<<"Updated Value: "<<num<<endl; return 0; }
The above program produces the following result:
Updated Value: 30
Example 2
Swapping two numbers using reference variables.
#include <iostream> using namespace std; void swap(int& a, int& b) { int temp = a; a = b; b = temp; } int main() { int x = 2, y = 9; swap(x, y); cout<<"x:"<<x<<", y:"<<y<<endl; return 0; }
The above program produces the following result:
x: 9, y: 2