C++ Call by Pointer

In C++, call by pointer passes the address of a variable to the function. The function can access and modify the original variable using the pointer.

1. Call by Pointer Syntax

The function receives a pointer to the argument using * in the parameter and can modify the original variable by dereferencing the pointer.

C++
Syntax of call by pointer
return_type function_name(data_type *parameter) {
    // function body
}

2. Call by Pointer Example

This example demonstrates call by pointer where changes inside the function modify the original variable.

C++
Example: Call by pointer
#include <iostream>
using namespace std;

void increment(int *a) {
    *a = *a + 1;
    cout << "Inside function: " << *a << endl;
}

int main() {
    int x = 5;
    increment(&x);
    cout << "Outside function: " << x << endl;
    return 0;
}

3. Common Mistakes

Passing a null pointer or forgetting to dereference the pointer (*) are common mistakes. Always pass valid addresses and dereference correctly inside the function.

Conclusion

C++ call by pointer allows functions to modify the original variable via its address. It is useful for dynamic memory, arrays, and when avoiding copies of large data.