In this session, we are going to understand the meaning & use of a generic and NULL pointer.
Generic pointer (void pointer)
The generic pointer is also called void pointer also. With pointer declaration, we have to specify its data type also. That data type is the same as the data type of that variable, to which that pointer is pointing. But in some applications, we have to use only one pointer that sometimes points to any data type variable and then points to any other data type variable. For that, we use a void pointer.
Let us discuss the syntax of defining and de-referencing a void pointer.
void *<pointer name> = & <variable name to which void pointer is pointing>;
De-referencing :
(in normal pointer for dereferencing, we use *<pointer name>) <variable name if we want to store value> = *( <data type of variable to which void pointer is pointing > *)<pointer name>;
Example program to understand the use of void pointer
In the below example, there is a void pointer ‘p’. Variable ‘a’ is of int type and has value ‘1’. Variable ‘b’ is of float type and has the value ‘2.5’. Firstly ‘p’ is pointing to variable ‘a’. As ‘a’ is of int data type, while printing value of ‘a’ (dereferencing pointer ‘p’) we are using *(int *)p. And after this ‘p’ is pointing to variable ‘b’. As ‘b’ is of float data type, while printing value of ‘b’ (dereferencing pointer ‘p’) we are using *(float *)p.
#include<stdio.h> #include<conio.h> void main() { void *p; int a=1; float b=2.5; clrscr(); p = &a; printf("a = %d \n",*(int *)p); p = &b; printf("b = %f \n",*(float *)p); getch(); }
Output:
NULL pointer
A NULL pointer is used when a pointer is not pointing to any valid memory address.
In some situations, we do not have any address which is to be assigned to the pointer so that is known as a null pointer. When we do not want to pass any valid address to any function then we pass a NULL pointer as an argument of the function.
In short, a NULL pointer has a NULL value (0). After that, we can use the NULL pointer to point to any valid address also.
Sometimes we want to run some code when there is a valid address of a pointer (pointer is not NULL). If there is no valid address assigned to the pointer (pointer is NULL) then it will run some other code. For that, we use a NULL pointer. Let us understand it by an example. We are using the pointer ‘p’.
Ex.
<code> if(p==NULL) { <code to run when there is no valid address assigned to pointer ‘p’> } else { <code to run when there is a valid address assigned to pointer ‘p’> }
Example program to prove that NULL pointer has a NULL value
In the below example, ‘p’ is a NULL pointer. If the value of ‘p’ is NULL (0) then it is proved that ‘p’ is a NULL pointer. And then, pointer ‘p’ is used to point variable ‘a’ which is of int data type and having value ‘1’.
#include<stdio.h> #include<conio.h> void main() { int *p=NULL; int a=1; clrscr(); printf("p = %d \n",p); p = &a; printf("p(address of a)= %d \n",p); printf("value of a = %d \n",*p); getch(); }
Output:
Learning from this blog:
- What is a generic and NULL pointer?
- What is the use of a generic pointer?
- Syntax to use a generic and NULL pointer.
- When to use a NULL pointer?
- How to write code for using NULL and generic pointers?