Passing by Pointer Vs Passing by Reference

C++ Passing by Pointer Vs Passing by Reference

In C++, when you pass an argument to a function by reference, you are actually passing a reference to the memory location of the argument. This page is al about Passing by Pointer VS Passing by Reference.

Whereas on the other hand, when you pass an argument to a function by pointer, you are passing a pointer to the memory location of the argument.

Passing by Pointer VS Passing by Reference

Pointer Vs Reference in C++

Passing by reference means that if you change the value of the argument inside the function, the change will be reflected outside the function as well. Whereas passing by pointer means inside the function, you can dereference the pointer to change the value of the argument, but the change will not be reflected outside the function unless you pass the pointer back as a return value.

Passing by pointer vs passing by reference in C++

Here is an example to illustrate the difference:

Run
#include <iostream>
using namespace std;

void incrementByPointer(int* x) {
  (*x)++;
}

void incrementByReference(int& x) {
  x++;
}

int main() {
  int a = 5;
  int b = 5;

  incrementByPointer(&a);
  incrementByReference(b);

  std::cout << "a: " << a << std::endl;
  std::cout << "b: " << b << std::endl;

  return 0;
}

Output

a: 6
b: 6

In general, it is recommended to use references rather than pointers when passing arguments to functions in C++, because references are easier to use and less error-prone. However, there are some situations where pointers are more appropriate, such as when you need to pass a null pointer or when you want to pass a pointer to an array.

Passing by pointer in C++

To pass an argument to a function by pointer in C++, you need to declare the function with a pointer parameter. The parameter will be a pointer to the type of the argument you want to pass. For example, to pass an int argument by pointer, you would declare the function like this:

void foo(int* x);

To call the function and pass an argument by pointer, you need to take the address of the argument using the & operator, and pass it to the function as follows:

int a = 5;
foo(&a);

Run

#include <iostream>
using namespace std;
void swap(int* a, int* b) {
   int c = *a;
   *a= *b;
   *b = c;
}
int main() {
   int m = 7, n = 6;
   cout << "Before Swap\n";
   cout << "m = " << m << " n = " << n << "\n";
   swap(&m, &n);
   cout << "After Swap by pass by pointer\n";
   cout << "m = " << m << " n = " << n << "\n";
}

Output:

Before Swap
m = 7 n = 6
After Swap by pass by pointer
m = 6 n = 7

Passing by Reference in C++

To pass an argument to a function by reference in C++, you need to declare the function with a reference parameter. The parameter will be a reference to the type of the argument you want to pass. For example, to pass an int argument by reference, you would declare the function like this:

void foo(int& x);

To call the function and pass an argument by reference, you can simply pass the argument as follows:

int a = 5;
foo(a);

Run

#include <iostream>
using namespace std;
void swap(int& a, int& b) {
   int c = a;
   a= b;
   b = c;
}
int main() {
   int m =7, n = 6;
   cout << "Before Swap\n";
   cout << "m = " << m << " n = " << n << "\n";
   swap(m, n);
   cout << "After Swap by pass by reference\n";
   cout << "m = " << m << " n = " << n << "\n";
}

Output:

Before Swap
m = 7 n = 6
After Swap by pass by reference
m = 6 n = 7

 

In general, it is recommended to use references rather than pointers when passing arguments to functions in C++, because references are easier to use and less error-prone.

However, there are some situations where pointers are more appropriate, such as when you need to pass a null pointer or when you want to pass a pointer to an array.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription