• C Programming Video Tutorials

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.

Syntax

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

void *ptr;

A void pointer cannot work with increment or decrement operators because it doesn’t have a specific type. Pointer arithmetic is not possible with a void pointer due to its concrete size.

Example

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: %d\n", *((int *)arr[0]));
   printf("Float: %f\n", *((float *)arr[1]));
   printf("String: %s\n", *((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

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.

Advertisements