Pointers

Pointer(also known as locator) is a variable that points to a memory address of a value. In simple terms, Pointers are symbolic representations of addresses. They enable programs to simulate call-by-reference as well as to create and manipulate dynamic data structures. Let us study this in detail with below code examples.

Before entering coding part we need to know two important terms.

CODE

EG 1 . Creating a pointer variable and assigning memory address of another variable.

#include <iostream>
using namespace std;

int main() {
    int a = 5;
    int* b = &a;
    cout << "Memory address of A = " << &A << endl;
    cout << "Address stored inside B = " << B << endl;
    cout << "Value stored inside B = " << *B << endl;
}

Pointer memory allocation

EG 2 . Creating a void method with pointer parameters.

#include <iostream>
using namespace std;

void ArithemeticCalculation(double num1, double num2, double* add, double* sub, double* mul, double* div);

int main() {
    double a=0, s=0,m = 0,d =0;
    ArithemeticCalculation(5,3,&a,&s,&m,&d);
    cout << a << endl;
    cout << s << endl;
    cout << m << endl;
    cout << d << endl;
}

void ArithemeticCalculation(double num1,double num2,double* add,double* sub,double* mul,double* div) {
    *add = num1 + num2;
    *sub = num1 - num2;
    *mul = num1 * num2;
    *div = num1 / num2;
}



(Click Here, For more exercises)