Back to C Programming
2026-07-155 min read

Void Pointers in C

Learn Void Pointers in C step by step with clear examples and exercises.

Why This Matters

In this guide, we will delve into the intricacies of void pointers in C, their importance, and how to use them effectively. Void pointers are an essential tool for advanced C programming tasks such as dynamic memory allocation, function pointers, and working with libraries that return generic data types. They help avoid typecasting errors, improve code readability, and provide a level of flexibility in handling various data types.

Prerequisites

Before diving into void pointers, it is essential to have a solid understanding of the following C programming concepts:

  • Variables and data types
  • Functions
  • Arrays
  • Pointers
  • Memory management in C, including dynamic memory allocation using malloc() and free()

Core Concept

A void pointer is a special type of pointer that can point to any data type. It has no specific data type associated with it, hence the name "void." Void pointers are declared using the keyword void*.

void *ptr; // Declaring a void pointer

Since a void pointer does not know the type of data it points to, you must always perform an explicit typecast when accessing or modifying the memory location it points to. This is done using a typecast operator (type).

int num = 42;
int *ptr = # // Correct pointer declaration and initialization
void *vptr = ptr; // void pointer pointing to an int
int *new_ptr = (int *)vptr; // Explicit typecast to access the int value
printf("%d\n", *(int*)vptr); // Prints 42

Void Pointers and Dynamic Memory Allocation

Void pointers are particularly useful when dealing with dynamically allocated memory. You can use a void pointer to allocate memory for any data type, then perform an explicit typecast to access the memory as needed.

void *memory = malloc(sizeof(int) + sizeof(char)*10); // Allocate memory for int and char array
int *integer_ptr = (int *)memory; // Cast void pointer to int pointer
char *string_ptr = (char *)(integer_ptr + 1); // Move past the int and cast to char pointer

*(int *)memory = 42; // Store an integer value
strcpy(string_ptr, "Example"); // Copy a string to the char array

Void Pointers and Structures

Void pointers can also be used with structures. To access specific members of a structure using a void pointer, you first need to cast it to the appropriate structure type and then use the dot notation to access the member.

typedef struct {
int id;
char name[20];
} Person;

Person person = { .id = 1, .name = "John Doe" };
void *ptr = &person;
Person *person_ptr = (Person *)ptr;
printf("%d %s\n", person_ptr->id, person_ptr->name); // Prints 1 John Doe

Worked Example

Let's create a simple program that demonstrates void pointers in action. This program will dynamically allocate memory for an integer and a character array using a void pointer, then use void pointers to access and manipulate them.

#include <stdio.h>
#include <string.h>

int main() {
// Allocate memory for int and char arrays using a void pointer
void *memory = malloc(sizeof(int) + sizeof(char)*10);

// Cast the void pointer to pointers for the integer and character arrays
int *integer_ptr = (int *)memory;
char *string_ptr = (char *)(integer_ptr + 1);

// Store an integer value and a string in the allocated memory
*(int *)memory = 42;
strcpy(string_ptr, "Example");

// Print the stored values using explicit typecasts
printf("Integer: %d\n", *(int *)memory);
printf("String: %s\n", string_ptr);

// Free the allocated memory
free(memory);

return 0;
}

When you run this program, it will output:

Integer: 42
String: Example

Common Mistakes

  1. Forgetting to perform an explicit typecast when using a void pointer: This can lead to typecasting errors and undefined behavior.
void *vptr = malloc(sizeof(int));
*(char *)vptr = 'A'; // Typecast error: char cannot be assigned to int
  1. Not freeing memory allocated using a void pointer: Failing to free dynamically allocated memory can lead to memory leaks.
void *memory = malloc(sizeof(int) + sizeof(char)*10);
// ...
// Forgetting to call free(memory) before program exit
  1. Misusing void pointers for function pointers: Void pointers should not be used as function pointers unless the function has a void return type and takes no arguments.
  1. Ignoring data alignment issues: When using void pointers, ensure proper data alignment to avoid potential crashes or unexpected behavior caused by misaligned memory accesses.

Common Mistakes (Continued)

  1. Incorrectly casting between different data types: Be mindful of the size and alignment differences between various data types when casting between them using a void pointer.
void *vptr = malloc(sizeof(float)); // Allocate memory for float
char *char_ptr = (char *)vptr; // Cast to char pointer
*(int *)char_ptr = 1234; // Typecast error: int cannot be assigned to char

Practice Questions

  1. Write a program that dynamically allocates memory for an array of 5 integers using a void pointer, then fills the array with numbers from 1 to 5.
  2. Create a function that takes a void pointer as an argument and prints its value using an explicit typecast. Test this function with different data types.
  3. Write a program that uses a void pointer to swap two variables of different data types without using temporary variables.
  4. Modify the worked example to dynamically allocate memory for a structure containing an integer and a character array, then print the contents of the structure using a void pointer.
  5. Investigate how to use void pointers with function pointers in C. Write a program that defines a function taking two arguments (an integer and a float) and returns their sum. Create a function pointer using a void pointer and call the original function through it.

FAQ

  1. Why can't I use void pointers for function pointers?

A: Void pointers are intended for generic memory allocation and access, while function pointers require specific knowledge of the function's return type and argument list. Using a void pointer as a function pointer can lead to undefined behavior.

  1. Is it safe to assign one void pointer to another without an explicit typecast?

A: No, you should always perform an explicit typecast when copying or manipulating the contents of a void pointer to ensure proper data alignment and avoid typecasting errors.

  1. Can I use void pointers for polymorphism in C?

A: While void pointers can be used for some forms of polymorphism, they are not as powerful or flexible as object-oriented programming languages like Java or C++. In C, you would typically achieve polymorphism using function pointers and virtual functions within a structure hierarchy.

  1. How do I handle data alignment issues when using void pointers?

A: To ensure proper data alignment, always use the correct size for each data type when allocating memory with malloc() or similar functions. Additionally, you can use platform-specific functions like posix_memalign() to control memory allocation alignment.