C – void Pointer


void Pointer in C



”;


void Pointer in C

A void pointer in C is a type of pointer that is not associated with any data type. A void pointer can hold an address of any type and can be typecasted to any type. They are also called general-purpose or generic pointers.

In C programming, the function malloc() and calloc() return “void *” or generic pointers.

Declaring void Pointer

This is the syntax you would use to declare a void pointer −


void *ptr;

Example of void Pointer

The following example shows how you can use a void pointer in a C program −


#include <stdio.h>

int main(){

   int a = 10;
   char b = ''x'';

   // void pointer holds address of int a
   void *ptr = &a;
   printf("Address of ''a'': %d", &a);
   printf("nVoid pointer points to: %d", ptr);

   // it now points to char b
   ptr = &b;
   printf("nAddress of ''b'': %d", &b);
   printf("nVoid pointer points to: %d", ptr);
}

Output

Run the code and check its output −


Address of ''a'': 853377452
Void pointer points to: 853377452
Address of ''b'': 853377451
Void pointer points to: 853377451

An Array of void Pointers

We can declare an array of void pointers and store pointers to different data types.

A void pointer is a pointer that can hold the memory address of any data type in C. Hence, an array of void pointers is an array that can store memory addresses, but these addresses can point to variables of different data types.

You can also store pointers to any user-defined data types such as arrays and structures in a void pointer.

Example

In this example program, we have declared an array of void pointers and stored in it the pointers to variables of different types (int, float, and char *) in each of its subscripts.


#include <stdio.h>

int main(){

   void *arr[3];

   int a = 100;
   float b = 20.5;
   char *c = "Hello";

   arr[0] = &a;
   arr[1] = &b;
   arr[2] = &c;

   printf("Integer: %dn", *((int *)arr[0]));
   printf("Float: %fn", *((float *)arr[1]));
   printf("String: %sn", *((char **)arr[2]));

   return 0;
}

Output

When you run this code, it will produce the following output −


Integer: 100
Float: 20.500000
String: Hello

Application of void Pointers

Some of the common applications of void pointers are listed below −

  • The malloc() function is available as a library function in the header file stdlib.h. It dynamically allocates a block of memory during the runtime of a program. Normal declaration of variables causes the memory to be allocated at the compile time.

     
    void *malloc(size_t size);
    

  • Void pointers are used to implement generic functions. The dynamic allocation functions malloc() and calloc() return “void *” type and this feature allows these functions to be used to allocate memory of any data type.

  • The most common application of void pointers is in the implementation of data structures such as linked lists, trees, and queues, i.e., dynamic data structures.

Limitations of void Pointer

The void pointer has the following limitations −

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *