Void Pointer |
Yes, we can use pointers without specifying the data type of the pointers. But remember at the end of the programming, when we will try to get the value of the data or a variable stored in that pointer, we need to declare the data type of the pointer in an indirect way.
So, before knowing the process, let's take a peek on what is
pointer?
POINTER:-
In C programming we use variables to store our desired
value. For this, the compiler allocates some memory for the variable to store our
desired value. So, when we try to get the value of that variable, we always
specify the data type of the variable. But we also can indirectly manipulate
or use the value stored in the variable by the use of this so-called POINTER.
How the process works:-
Pointer stores the address of the variable that we want to
use it through a pointer. And by linking the address of that variable with the
pointer, we can use and manipulate the data saved in the variable.
Let's take an example to see how the whole process is going
on-
Programming code:-
#include<stdio.h>
int main ( )
{
int x = 30, *ptr ;
ptr = &x ;
printf ( "%d", *ptr ) ;
}
output:-
30
variable value (x)
|
30
|
address of the variable
|
2424388
|
pointer value (*ptr)
|
2424388
|
address of the pointer
|
2424300
|
NOTE:- Every time we take a pointer, the data type of the pointer should be the same as that of the data type of the variable.
Now getting into our main question how can we use pointers without specifying its data type?
In C programming, we use integer, floating-point
, character, etc. data types. But there is
another data type “VOID”. We can say this is a data type with no data type. You
can say what is that means?
If we take an example :
void name;
this implies the data type of the variable ‘name’ is not
specified whether it is an integer, floating-point, or character data type.
- Void means ‘null’.
- By using this ‘void’ we can declare a pointer without specifying its data type and later we can get the value of the variable that is stored in that pointer.
By the following programming code, we can do that-
Programming Code |
Output:-
Enter a number: 10
The entered number is 10
Let's see how the programming code works:
- First, we have taken a variable of an integer data type.
- Then, we have taken a ‘void’ pointer as “*ptr”. (‘*’ denotes that ‘ptr’ is a pointer). So, the data type of the pointer is not declared.
- Next, we have asked the user to give input by the use of ‘scanf’ function.
Last and the main part:-
printf(“The entered number is %d”,*(int *)ptr);
- by the argument “*(int *)ptr”, we are declaring that the data type of the variable stored in the pointer is of ‘integer’ type.
- As I have already told that when we want to get the value of the variable through a pointer, we must declare the data type at that time otherwise, the compiler will give an error.
- By the expression “(int *)”, the compiler understands that the variable is of integer type and if we use only this expression, then the address of the variable will be printed instead of its value.
Prints the address of the variable |
Output:-
The entered number is 2424388
- And by “*(int *)” this expression, compiler prints the value of the variable that we wanted to use it through the pointer.
Hope it has helped you.
If any doubt persists, please comment below. Thank you for
reading.