Void pointer

A void pointer can hold address of any type and can be typecast to any type and in this pointer which is not associated with any data types. In C malloc() and calloc() functions return void pointer and so these functions can be used to allocate the
memory of any data type.The void pointer also be used to implement the generic function in C.

Syntax of void pointer

void*pointer name;

Declaration of the void pointer

void*ptr;

Let’s see the example of size of the void pointer in C

#include <stdio.h>  
int main()  
{  
    void *ptr = NULL;   
    int *p  = NULL;  
    char *cp = NULL;  
    float *fp = NULL;   
    printf("size of void pointer = %d\n\n",sizeof(ptr));    
    printf("size of integer pointer = %d\n\n",sizeof(p));   
    printf("size of character pointer = %d\n\n",sizeof(cp));    
    printf("size of float pointer = %d\n\n",sizeof(fp));  
    return 0;  
}

Output

size of void pointer = 8
size of integer pointer = 8
size of character pointer = 8
size of float pointer = 8

Let’s see the malloc() example of void pointer

#include <stdio.h>  
#include<malloc.h>  
int main()  
{  
   int a=90;  
   
   int *x = (int*)malloc(sizeof(int)) ;  
   x=&a;  
   printf("Value which is pointed by x pointer : %d",*x);  
    return 0;  
}

Output

Value which is pointed by x pointer : 90

Submit your review
1
2
3
4
5
Submit
     
Cancel

Create your own review

The Technical Funda
Average rating:  
 0 reviews