Register Login

Void pointers and null pointers

Updated Sep 11, 2021

Pointers are a special type of variable that can store the address of another variable. Such types of variables can be integer, floating-point, character, arrays, functions, etc. Declaring and initializing a pointer is very essential as they work with the addresses of the memory directly. In this article, you will learn about the difference between the null pointer and the void pointer.

What is a null pointer?

A null pointer is a kind of pointer that does not point to any memory location. If you declare a pointer and do not initializes it, then there is a possibility of data getting leaked  or accessed by other programs. Also, this is not a good programming approach to declare a pointer variable and not initializing it.

Program:

#include <iostream> 
using namespace std;
int main(){  
    Int * ptrr = NULL;  
    if(ptrr != NULL){  
        Cout << " The value in the pointer is: %d " << * ptrr;  
    }else{  
        Cout << " This is an invalid pointer ";  
    }
  return 0;  
}

What is a void pointer?

A void pointer is a generic pointer that has the ability to store the address of a variable of any data type. It is not associated with any data type and acts as a general-purpose pointer variable.

Program:

#include <iostream>  
using namespace std;  
int main(){  
  void * ptr;   
  int a = 9;  
  float b = 3.2;
  ptr = &a;
  cout << ptr << std :: endl;  
  ptr = &b;
  cout << ptr << std :: endl;
  return 0;
}  

Null pointer vs. Void Pointer

Null Pointer Void Pointer
It does not point to any address of any type. It can point to any address of any type.
Null pointers are created using the null keyword with the assignment operator. Void pointers are created using the void keyword in the place where the data type is placed.
It does not hold any address. It has the capability to hold any generic address.
It has to be of any fixed data type. It does not have any fixed data type and can store the address of a variable of any data type.
It is faster compared to void pointer. It is generic in nature and hence slower as compared to null pointers.
It remains null until an address is assigned to the variable. It remains void until any data type with fixed address gets assigned to it.
It does not refer to any address or memory location. It has to assign a memory and must refer to a memory location.
Declaring a pointer variable and not initializing it (not even with null) is a bad programming approach. It is for the flexibility of the programmer and has nothing to do with the programming approach.
It has nothing to do with typecasting. Keeping the pointer type as void exclude programmers from typecasting a pointer variable repetedly.

Conclusion:

Although both are two different concepts of the same topic, both are useful in their own respective situation. It is also necessary to understand the situation where to use null and where to use void. If you are planning to store different types of addresses of variables of different data types, use void. Again, if you want to declare a pointer but do not want to initialize it with any memory address, initialize it with null.


×