C++ Pointers

Pointer is a special variable used to store address of another variable.

Pointers in C++

Before beginning to learning about pointers, we need to understand how the variables are managed in a program.

Little info about variables

In the c++ programming language, we use normal variables to store user data values. When we declare a variable, the compiler allocates required memory with the specified name. In the c++ programming language, every variable has a name, datatype, value, storage class, and address.

In c++ programming language, we use the reference operator "&" to access the address of the variable. For example, to access the address of a variable marks, we use &marks. Look at the following code to display the memory location address of a variable marks.

Example code
#include <iostream>

using namespace std;

int main()
{
    int marks = 80;

    cout << "Address of marks -> " << &marks << endl;
    cout << "Value of marks -> " << marks << endl;

    return 0;
}
Output
program for displaying address

What is a pointer?

We use a special type of variable called a pointer to store the address of another variable with the same datatype. A pointer is defined as follows...

Pointer is a special type of variable used to store the memory location address of a variable.

In the c++ programming language, we can create pointer variables of any datatype. Every pointer stores the address of the variable with the same datatype only. That means the integer pointer is used to store the address of the integer variable only.


How to create a pointer in C++?

In c++ programming language, the declaration of a pointer variable is similar to the variable creation, but the name of the pointer variable must prefix with the symbol *. We use the following syntax to declare a pointer variable.

Syntax
data_type *pointerName;

A variable declaration prefixed with * symbol becomes a pointer variable.

Example
int *ptr;

In the above example declaration, the variable "ptr" is a pointer variable that holds any integer variable address.


How to use a pointer?

The pointer variable used to store the address of another variable of the same data type. It is also used to access the address and value of the variable to which it points. Look at the following code to illustrate the pointer use.

Example code
#include <iostream>

using namespace std;

int main()
{
    int marks = 80, *ptr;

    ptr = &marks;   //Assigning address to pointer

    cout << "Address of marks -> " << ptr << endl;
    cout << "Value of marks -> " << *ptr << endl;

    return 0;
}
Output
program to illustrate pointer use

Pointer Memory Allocation

Every pointer variable is used to store the address of another variable. In computer memory address of any memory location is an integer value. In c programming language, integer requires 4 bytes of memory. So, irrespective of pointer datatype every pointer variable is allocated with 4 bytes of memory.

Example code
#include <iostream>

using namespace std;

int main()
{
    int *intPtr;
    double *doublePtr;
    char *charPtr;

    cout << "Memory size of integer pointer intPtr -> " << sizeof(intPtr) << endl;
    cout << "Memory size of double pointer doublerPtr -> " << sizeof(doublePtr) << endl;
    cout << "Memory size of character pointer charPtr -> " << sizeof(charPtr) << endl;

    return 0;
}
Output
program to illustrate pointer use

void Pointer

In C++, void pointer is a pointer variable that is independent of data type. A void pointer is used to store the address of a variable of any datatype. We use the keyword "void" to create void pointer.

Example code
#include <iostream>

using namespace std;

int main()
{
    int intVariable;
    float floatVariable;

    void *anyPtr;

    anyPtr = &intVariable;

    cout << "Address of integer variable -> " << anyPtr << endl;
    cout << "Address of the pointer itself -> " << &anyPtr << endl;

    anyPtr = &floatVariable;

    cout << "Address of float variable -> " << anyPtr << endl;
    cout << "Address of the pointer itself -> " << &anyPtr << endl;

    return 0;
}
Output
program to illustrate void pointer use