×

Call by Pointer in C++

What is Pointer?

Every variable in C++ has a specific address or location in the computer's memory, and this address is known as the memory address. A pointer can be defined as a variable that stores the memory address of another variable.

 It enables the programmer to manage the memory.

The C++ language has a wide range of pointer-based features.

It has characteristics like the ability to refer to the same memory location from several memory locations, which means that we can update one point in the program and have the changes appear in other areas.

It is feasible to have a more flexible memory allocation and deallocation because of pointers. It's also utilized in function pointers and polymorphism.

Call by Pointer:

A function's address is copied into a formal parameter when arguments are passed to it using the call-by-pointer method. The address is used inside the function to determine the actual parameter used in the call. The passed argument is affected by modifications made to the parameter, in other words.

Argument pointers are supplied to functions the same way as other values to transmit values by the pointer. Since the following function swap() swaps the values of the two integer variables pointed to by its arguments, you must specify the function parameters as pointer types.

Due to the fact that both variables' addresses are the same, the variable inside the function is an alias for the variable that was supplied. As a result, any changes made to the variable within the function will likewise be reflected in the calling function.

A function that has the capacity to reflect changes may return more than one value. Additionally, this technique might theoretically allow a void function to return a value or several values. Values passed through the pass-by-pointer in a function are indicated by the & (address of) operator in the actual arguments.

Function Call by Pointer:

The term "call-by-pointer" refers to a technique where the variable address from the calling function is passed and used as a pointer inside the function.

Using this way, functions where the given variables' values could change are more clearly seen.

Specifically, with a pass-by-reference implementation, the function call does not contain any information that could be exploited to infer that the supplied variables' values might change during its execution (as the call is identical to call by value).

The address operator & is used in the function call when calling via a pointer, giving additional information about the function's purpose.

Program:

#include <iostream>
using namespace std;


// prototype for a function that takes pointers as parameters
void swap(int*, int*);


int main()
{


    // initialize variables
    int x = 12, y = 14;


    cout << "Before swapping" << endl;
    cout << "x = " << x << endl;
    cout << "y = " << y << endl;


    // calling a function with a variable address
    swap(&x, &y);


    cout << "\nAfter swapping" << endl;
    cout << "x = " << x << endl;
    cout << "y = " << y << endl;
    return 0;
}


//To swap numbers, here we define the function
void swap(int* a, int* b ) {
    int temp;
    temp = *a;
    *a = *b;
    *b = temp;
}

Explanation:

  • First, we created a swap function, which takes pointers as a function parameter. This function has void data types, which means this function is Returns Nothing type.
  • In the main() function, we initialize two variables, "x" and "y," which have an int data type.
  • Then we called the swap function with the address of the (&) operator.
  • Then We define the swap function for swapping the number.
  • When calling a function, the address of the variable is provided rather than the variable itself.
  • A dereference operator * must be used to access the value stored in the address since it is being passed rather than the value.
  • The value kept at addresses a and b, respectively, is given by the functions *a and *b.
  • Because a and b hold the addresses for x and y, any changes made to *a and *b will alter the actual values of x and y. Due to this, the values are altered when we print the values of x and y in the main() function.

Program output:

Call by Pointer in C++

Program:

#include <iostream>
using namespace std;


// Function define


void fun(int *a)
{
*a = *a - 1;
}




int main()
{
int x = 5;
cout << "Value of x before modification:  "<< x << endl;


// function call
fun(&x);
cout <<"Value of x after modification:  "<< x << endl;
}

Explanation:

  • 1st we define a function whose name is "fun" with the return type void and takes in value as a pointer. Here pointer (*) is denoted by an asterisk sign in the formal parameter.
  • In the fun() function body, the value of the formal parameter(*a) is decreased by 1.
  • In the main() function, we initialize a variable "x" with the value of 5, which has an int data type.
  • The execution will start from the main() function, the line "value of x before modification" will execute first, and the value of x will display.
  • Then the fun() function called the address of the variable is passed as an argument, and inside the function, the value of pointer a will decrease by 1.
  • Then the execution of this function will finish and execution takes back to the main() function.
  • And upon returning from the function, “the value of x after modification” will display, which turns out to be 1 less than the original value.

Program Output:

Call by Pointer in C++

Conclusion:

  • Call-by-pointer is quick and effective even when passing huge data items because a pointer or address' size is tiny and fixed (for instance, the size of a pointer to an int is the same as the size of a pointer to an enormous structure).
  • Programs can use the arguments to pass data into and out of functions using call-by-pointer technology (i.e., data can flow into the function, out of the function, or flow in both directions).
  • In C++, a call by the pointer is a technique for passing values to function parameters. When using a pointer to call a function, the formal parameter receives the address of the actual arguments. This means that if the values inside the function are changed, the actual parameters will also be affected.

Related Topics

Parameterize Constructor

C++ Parameterized Constructor A constructor having parameters is known as parameterize constructor. Parameterize constructor is used to assign different values. Syntax: className(data-type argument){   // Constructor definition   }   className(data-type argument, data-type argument){   // Constructor definition   } A parameterized constructor can be passed values to constructor function in two ways: 1)...

1 minute 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.

C++ Program to Implement Shell Sort

    C++ Program to Implement Shell Sort shell sort is basically an Insertion Sort variant. In the insertion sort, we only transfer elements ahead of one location. Many movements are involved...

2 minutes read.

Binary Operator Overloading in C++

The Binary Operator Overloading in the C++ programming language will be covered in this part. An operator which comprises two operands to execute a mathematical operation is termed the Binary...

6 minutes read.

Approach in C++

Object oriented programming languages like Java or C++ use a bottom-up approach that identifies each object first.  In the bottom-up approach, we create a small problem first, and try to...

6 minutes read.

Queue in C++

What is Queue? As the name suggests, the queue is the type of data structure that follows the FIFO (First In - First Out) mechanism. In simple words, it is...

4 minutes read.

Hospital Management Project in C++

The following capabilities are required to build a hospital management project: These are as follows: hospitals' names, contact information, and lists of doctors and patients. Activities Supported Hospital Data Print Patients' data to...

4 minutes read.

Template Specialization in C++

Template is a feature of C++. With the help of a template, we can write the code only once and use that code multiple times. For example, there is a...

4 minutes read.

C++ Close file

C++ File Handling close() Function A file which is opened while reading or writing in file handling must be closed after performing an action on it. The close() function is used to close...

2 minutes read.

How to create a button in C++

A button is an option through which a user can control to click a provided input to an application. There are several buttons, each with different styles, to maintain the...

3 minutes read.

C++ Keywords

In this article, we will discuss keywords in C++ with their several features and functions. What are Keywords in C++? In C++, a keyword is a reserved word that has a predefined...

4 minutes read.

Is it fine to write void main() or main() in C/C++?

In C programming language: The default function return type in the C programming language is "int," which implies that main() will always return an integer value. In C, the void main()...

3 minutes read.

Find the Size of Array in C/C++ without using sizeof() function

We know that arrays in C/C++ are the most essential data structures as they have the ability to hold the data in a continuous manner line where the address of...

3 minutes read.

C++ History

C++ is a middle-level programming language developed in 1980s by Bjarne Stroustrup at Bel Labs. C ++ development initially started in 1979, four years before its launch. It is started with...

1 minute read.

C++ Program to Print Fibonacci Triangle

Fibonacci Triangle Program in CPP Definition: Fibonacci Triangle as the name suggests is the same as the Fibonacci number series where the next element is the sum of the previous two elements....

3 minutes read.

INT_MAX and INT_MIN in C/C++

In competitive programming, assigning a variable that maximum or minimum value a data type can carry is frequently necessary. Still, it might be challenging to recall such a significant, exact...

3 minutes read.

abs() function in C++

In C++, the abs() function returns the absolute value of any integer number. Using this function a negative integer is multiplied by -1 and positive number or zero is returned...

4 minutes read.

Virtual base class in C++

Consider in a C++ program, there are 4 classes named class A, class B, class C, and class D. If class B and class c inherit properties from class A....

3 minutes read.

Inheritance Program in C++

What is Inheritance? Inheritance is the ability of a class to inherit traits and properties from another class. One of the most crucial aspects of Object-Oriented Programming is inheritance. The ability or...

9 minutes read.

Naming Convention in C++

The first and most fundamental step a programmer takes to produce clean code is to name a file or a variable. This naming must be acceptable so that it serves...

5 minutes read.