Pointers C++

Pointers C++

What are pointers?

The pointer variable points to a data type of the same type(like int, char etc. ). The address of the variable we are working with is assigned to the pointer.

In other words, a pointer is a variable that stores the memory address as its value.

A pointer can be created using the * operator.


Syntax:

datatype *variable_name;

Code:

#include<iostream>
using namespace std;
 int main(){
    int a[6] = {1, 2, 3};

    cout << (a + 2)<<endl; //gives address of 3rd element

    cout<<*(a+2); //gives value of 3rd element
}

Uses of Pointer:

  • Creation of dynamic data structures

  • Construction of different data structures like linked lists, queues, stacks etc.

  • Iteration over array elements.


Advantages:

  • Can access memory location of Computer memory.

  • Pointer increases the execution speed of a program.

  • Pointers can return multiple variables from the function.

  • Reduces size of code.


More about Pointers

Character pointer

We have learned what pointers are and what they are used for. Pointers are variable that stores the address of another variable. Character pointers means a pointer will be pointing to the address of a variable of character data type.

Code:

#include <iostream>
using namespace std;
int main()
{
    int a[] = {1, 2, 3};
    char b[] = " a b c"; // character pointer

    cout << a << endl; // gives address of a
    cout << b << endl; // gives value from b[0] till null
}

Pointers to Pointers

We have learned what pointers are and what they are used for. Pointers are variable that stores the address of another variable. Pointers to pointers means a pointer will be storing the address of another pointer.

Pointers to pointers are also known as double-pointers.

Code:

#include <iostream>
using namespace std;

int main()
{
    int i = 10;
    int *p = &i;// points to the address of i
    int **p1 = &p;// double-pointer. points to the address of pointer p

    cout << i << endl; //gives value of i
    cout << *p << endl; //gives value of i
    cout << **p1 << endl; //gives value of i
}

Void Pointers

When we use void in the declararion of a pointer, void specifies that the pointer is "universal".

Void pointer points to some data location in storage, and is not associated with any data types. It is generic in nature and remains void until any data type of fixed address is assigned to it.

Code:

#include<iostream>
using namespace std;
int main(){
    int a = 5;
    void* ptr = &a;

    cout << * ptr << endl;// cannot be dereferenced.
    cout << *(int*)ptr << endl; // dereferenced using explicit type casting
}

Null Pointers

Null pointer is faster as compared to the void pointer. Null pointer remains null until an address is assigned to the variable, it does not refer to any address or memory location.

Null pointer has to be of any fixed data type.

Code:

#include<iostream>
using namespace std;
 int main(){
    int* p = NULL; // null pointer
    int** ptrp = &p: // pointer to null pointer
}

When should we use Null Ponter and Void pointer?

Even though both Null and Void are two distinctive principles of the identical topic, both are useful in their very own respective scenario. It is also important to apprehend the situation in which to use null and where to use void.

If you want to store different types of addresses of variables of different data types, use void pointers.

If you want to declare a pointer but do not want to initialize it with any memory address, initialize it with null pointer.