×

Passing by Reference Vs. Passing by the pointer in C++

 Passing by Reference Vs. Passing by the pointer in C++

Throughout C++, it can transfer parameter values except by pointers or through referring to a function. For both cases, we have the same result. So the remaining statements are inevitable; when one is over the other significant? What are the causes we’re using one over another?

Passing by Pointer:

// C++ program for swaping two numbers with pass-by-pointer.
#include <iostream>
using namespace std;
void swap(int* t, int* p)
{
    int k = *t;
    *t = *p;
    *p = k;
}
int main()
{
    int s = 78, c = 55;
    cout << "Before Swap\n";
    cout << "s = " << s << " c = " << c << "\n";
    swap(&s, &c);
    cout << "After Swap with pass by pointer\n";
    cout << "s = " << s << " c = " << c << "\n";
}

Output:

Passing by Reference vs Pointer in C++

Passing by Reference:

// C++ program to swap two numbers using
// pass by reference.
#include <iostream>
using namespace std;
void swap(int& w, int& q)
{
    int l = w;
    w = q;
    q = l;
}
int main()
{
    int n = 122, m = 210;
    cout << "Before Swap\n";
    cout << "n = " << n << " m = " << m << "\n";
    swap(n, m);
    cout << "After Swap with pass by reference\n";
    cout << "n = " << n << " m = " << m << "\n";
}

Output:

Passing by Reference vs Pointer in C++

The difference in between Reference variable and pointer variable

  • Generally, references are applied with pointers. A reference is the same object; it only has to refer to an entity with a different name and context. They are safer to use as references cannot be NULL.
  • A pointer may be reassigned while the relation cannot be assigned and must only be allocated at initialization.
  • The pointer is explicitly assignable to NULL, while reference cannot.
  • Pointers may append over an array, and we can use + + to move towards the next object a pointer points toward.
  • The variable is a way that has an address to the memory. A reference has the same address as the object it is referring to.
  • A class or struct pointer requires' ->'(arrow operator) to reach its members while a reference uses a. '(dot operator)
  • To access the memory location it refers to, a pointer must be dereferenced with *, while a pointer can be substituted directly.
// C++ program to display variations between reference and pointer.
#include <iostream>
using namespace std;
struct demo
{
    int s;
};
int main()
{
    int m = 3;
    int n = 8;
    demo d;
    int *k;
    k =  &m;
    k = &n;                     // 1. Pointer reintialization allowed
    int &t = m;
    // &r = y;                  // 1. Compile Error
    t = n;                      // 1. m value becomes 3
    k = NULL;           
    // &r = NULL;               // 2. Compile Error
    k++;                        // 3. Points to next memory location
    t++;                        // 3. m values becomes 8
    cout << &k << " " << &m << endl;    // 4. Different address
    cout << &t << " " << &m << endl;    // 4. Same address
    demo *q = &d;
    demo &qq = d;
    q->s = 9;
    // q.s = 9;                 // 5. Compile Error 
    qq.s = 9;
    // qq->s = 9;               // 5. Compile Error
    cout << k << endl;       
    cout << t << endl;            
    return 0;
}

Output:

Passing by Reference vs Pointer in C++

Related Topics

Converting string into integer in C++

When programming in C++, we'll frequently need to change one data type to another. When we use C++ to create apps, we must transform data from one type to another. When...

7 minutes read.

Decimal to Octal in C++

We must create a software that converts a decimal number into an equal octal number given a decimal number as input i.e. convert a number having a base value of...

3 minutes read.

CPP Templates

C++ provides a powerful feature called template, which allows the definition of generic classes and generic functions. Generic programming is a technique where different algorithms work in communion by using...

4 minutes read.

Difference between Exit and Return

Define Exit() At the point when a client needs to leave a program from this capability is utilized. A void return type capability calls all capabilities enrolled at the exit and ends...

3 minutes read.

Scope Resolution Operator in C++

The scope resolution operator and its different usage in the C++ programming language will be discussed in this section. The scope resolution operator is used to refer to an out-of-scope...

6 minutes read.

Structure Sorting (By Multiple Rules) in C++

To understand the concept of Structure Sorting (By Multiple Rules) in C++, it is recommended to know the Structures concept in the C++ programming language. Here the scenario is pretty...

3 minutes read.

C++ String Class and its Applications

The String class is available in C++. The character array is represented by the C string. The string class in C++ has a few different attributes. It contains several functions...

4 minutes read.

Substring in C++

A substring is a function in c++ that is imported from the string library. An element of a string is called a substring. A substring contains two parameters length and...

2 minutes read.

Pattern programs in C++

In this article, we are going to discuss different pattern programs in C++. Program for Printing * patterns: Right Angle Triangle* pattern #include <iostream> using namespace std; int main() {     int rows;     cout <<...

3 minutes read.

Print Table Using Do while Loop in C++

Multiplication Table In mathematics, a table is created by multiplying a certain number by all of the counting numbers, i.e., 1, 2, 3, 4, 5, 6, and so on. It is...

4 minutes read.

Add two numbers represented by two arrays in C++

The array stores a number in such a way that each digit of the number is represented by an array element. As an example, The array number 147 is 1,4,7. To add...

3 minutes read.

Multiple Inheritance

C++ Multiple Inheritance Multiple inheritance is such an inheritance in which a derived class inherits properties of more than one base class.     C++ Multiple Inheritance Example In this example, two base classes Square and...

2 minutes read.

C++ User Defined Exceptions

Overriding and inheriting exception class capabilities may be used to define the new exception. Exception handling can also be used with classes. We may also make an exception for user-defined...

4 minutes read.

C++ Fork

A new process known as a "child process" is created with the fork system function and runs concurrently with the process that invoked fork() (parent process). Both processes will carry...

4 minutes read.

4 Pillars of OOPs

OOPs OOPS means Object Oriented Programming System Structure. Object oriented Programming is defined as an approach that provides a way of modularizing programs by creating partitioned memory area for both data...

11 minutes read.

Default Constructor

C++ Default Constructor A default constructor is such constructor which does not take any parameter. A constructor initializes the data member when a class object created. If a programmer does not create any...

2 minutes read.

Delete Operator in C++

Overview We can reserve allocation for a variable or an array at runtime in C++. Dynamic memory allocation is the term for this. After utilising a variable in C++, we must...

4 minutes read.

Snake Code in C++

Snake is a popular game that can be played on almost any device and runs on any operating system. In this game, snakes can move in any direction, including left,...

4 minutes read.

Ascending order in C++

In C++, the term "ascending order" refers to a specific order in which a list of elements is arranged. When a list of elements is arranged in ascending order, the...

3 minutes read.

Char Array to String in C++

Regardless of the programming language you use, data structure is critical to the success of your project. Although each programming language has its own collection of data structures, C++ contains a...

4 minutes read.