Void pointer
It is the type of pointer which points to some data location in storage/memory which doesn't have any specific type. Such a pointer can point to any type of data, that is, any pointer type is convertible to a void pointer. Void pointers are also known as general-purpose pointers. There are a few limitations to void pointers:
- Void pointers can not be used for pointer arithmetic because they are of a concrete size.
- It cannot be dereferenced. However, it can be done using typecasting the void pointer.
Advantages
- Functions such as malloc() and calloc() return void* type, which allows such functions to allocate memory of any data type
-
These pointers are also used to implement generic functions in the C programming language.
Code
#include<stdlib.h>
#include <cstdio>
int main()
{
int var1 = 100;
float var2 = 120.5;
//declaring and intializing a void pointer
void *ptr;
ptr = &var1;
// perform Type-casting of void by (int*)ptr
// void pointer variable
printf("Integer value of variable using the void pointer is = %d", *( (int*) ptr) );
// void pointer is now float
ptr = &var2;
printf("\nFloat value of variable using the void pointer is = %f", *( (float*) ptr) );
return 0;
}
Output
Integer value of variable using the void pointer is = 100
Float value of variable using the void pointer is = 120.500000
Explanation
In the above example, we have first created a void type pointer named “ptr”, and it is assigned the address of variable “var1” which is of integer type. We have used the void type pointer to print the variable “var1” value by performing typecasting. Then, the void pointer “ptr” is assigned the address of variable “var2” which is of float type. And then, using the typecasting, we have used the void pointer to print the variable “var2” value.
Null pointer
It is the type of pointer which points to nothing, it is useful in cases when we don't have an address which is to be assigned to a pointer. Some important points regarding null pointers are:
- A null pointer is a value, whereas a void pointer is a type
- A null pointer stores a defined value, whereas uninformed pointer stores an undefined value.
- A null pointer can be used to initialize a pointer variable when the memory address which is to be assigned is unknown.
-
It can also be used to pass a null pointer to a function argument if we wish not to pass any valid memory address.
Code
#include <stdio.h>
int main()
{
// declaring a null-pointer
int *nul_ptr = NULL;
printf("The value of ptr is : %p", nul_ptr);
return 0;
}
Output
The value of ptr is : (nil)
Wild Pointer
It is a type of pointer that has not been initialized to anything, not even a NULL value.
Code
#include <stdio.h>
int main()
{
// declaring a wild pointer
int *ptr;
int val = 47;
// ptr is not a wild pointer anymore
ptr = &val;
return 0;
}
Frequently Asked Questions
What are pointers?
A pointer is a type of variable whose value is the address of another variable.
What is the difference between a null pointer and a void pointer?
The difference between a null and a void pointer is that a null pointer is a value, whereas the void pointer is a type.
What is the disadvantage of using void pointers?
The main disadvantage of using void pointers is that they cannot be used for pointer arithmetic.
Conclusion
In this article, we have extensively discussed the different types of pointers, such as Dangling, Void, Null and Wild pointers.
After reading about dangling, void, null and wild pointers, are you not feeling excited to read/explore more articles on pointers? Don't worry; Coding Ninjas has you covered. To learn about pointers and references, what is the size of a pointer in C, and how to clone a linked list with a random pointer.
Recommended Reading: AMD vs Intel
If you wish to enhance your skills in Data Structures and Algorithms, Competitive Programming, JavaScript, and many more, then you should check out our Guided path column at Coding Ninjas Studio. We at Coding Ninjas Studio organize many contests in which you can participate. You can also prepare for the contests and test your coding skills by giving the mock test series available. In case you have just started the learning process, and your dream is to crack major tech giants like Amazon, Microsoft, etc., then you should check out the most frequently asked problems and the interview experiences of your seniors that will surely help you in landing a job in your dream company.
Do upvote if you find the blogs helpful.
Happy Learning!