Back to C Programming
2026-07-145 min read

C - void Pointer

Learn C - void Pointer step by step with clear examples and exercises.

Why This Matters

Understanding the concept of void pointers in C programming is crucial for mastering dynamic memory management and function arguments. The void pointer plays a significant role when dealing with generic functions or handling unknown data types. It helps avoid unnecessary type casting and improves code readability. By learning how to use void pointers, you'll be better equipped to write more flexible and reusable C programs.

Prerequisites

Before diving into the core concept of void pointers, it is important that you have a good understanding of the following:

  • Basic C syntax and data types (int, char, float, etc.)
  • Pointers in C (declaration, dereferencing, and pointer arithmetic)
  • Functions in C (definition, calling, and passing arguments)
  • Data Structures (arrays, structures, and linked lists)
  • Standard Library functions (printf(), scanf(), malloc(), free(), memcpy(), etc.)

Core Concept

A void pointer is a generic pointer that can hold the address of any data type. It does not specify the type of data it points to, which makes it useful for functions that handle data of unknown types or when you want to return multiple values from a function. In C, void pointers are declared as:

void* pointer_name;

Dereferencing and Assignment

To dereference a void pointer, you can use the * operator just like with other pointers. However, when assigning values to or from a void pointer, you must explicitly cast it to the appropriate data type:

int num = 42;
float flt = 3.14;
char str[] = "Hello";
void *ptr;

ptr = # // Assign the address of num to ptr
int *ptr_casted = (int *)ptr; // Cast ptr back to int pointer and assign to ptr_casted
printf("%d\n", *ptr_casted); // Print the value stored at the memory location pointed by ptr

ptr = &flt; // Assign the address of flt to ptr
float *flt_casted = (float *)ptr; // Cast ptr back to float pointer and assign to flt_casted
printf("%f\n", *flt_casted); // Print the value stored at the memory location pointed by ptr

ptr = &str; // Assign the address of str to ptr
char *str_casted = (char *)ptr; // Cast ptr back to char pointer and assign to str_casted
printf("%s\n", str_casted); // Print the value stored at the memory location pointed by ptr

Functions with void Pointers

Functions that accept or return void pointers can be useful for handling data of unknown types. For example, a function to swap two variables of any type:

void swap(void *a, void *b, size_t size) {
void *temp = malloc(size); // Allocate memory to store the value of one variable temporarily
memcpy(temp, a, size); // Copy the value from 'a' to temp
memcpy(a, b, size); // Copy the value from 'b' to 'a'
memcpy(b, temp, size); // Copy the value from temp back to 'b'
free(temp); // Free the temporary memory
}

Using void Pointers with Data Structures

You can also use void pointers with data structures such as arrays and structs. For example, consider a simple struct:

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

Person p1 = { .id = 1, .name = "John" };
void *ptr = &p1; // Assign the address of p1 to ptr
Person *person_casted = (Person *)ptr; // Cast ptr back to Person pointer and assign to person_casted
printf("%d %s\n", person_casted->id, person_casted->name); // Print the id and name stored in p1

Worked Example

Let's create a simple program that demonstrates using void pointers with arrays:

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

// Function to swap two elements of an array of any type
void swap(void *arr, size_t size, size_t index1, size_t index2, size_t elementSize) {
void *temp = malloc(elementSize);
memcpy(temp, *(void **)((char *)arr + index1 * elementSize), elementSize);
memcpy(*(void **)((char *)arr + index2 * elementSize), temp, elementSize);
memcpy(*(void **)((char *)arr + index1 * elementSize), *(void **)((char *)arr + index2 * elementSize), elementSize);
free(temp);
}

int main() {
int arr_int[] = { 1, 2, 3, 4, 5 };
float arr_flt[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f };
char arr_char[6] = { 'H', 'e', 'l', 'l', 'o', '\0' };

printf("Before swapping:\n");
printf("arr_int: ");
for (size_t i = 0; i < sizeof(arr_int) / sizeof(arr_int[0]); ++i) {
printf("%d ", arr_int[i]);
}
printf("\n");

printf("arr_flt: ");
for (size_t i = 0; i < sizeof(arr_flt) / sizeof(arr_flt[0]); ++i) {
printf("%f ", arr_flt[i]);
}
printf("\n");

printf("arr_char: %s\n", arr_char);

// Swap the second and third elements of each array using void pointers
size_t elementSize_int = sizeof(int);
size_t elementSize_flt = sizeof(float);
size_t elementSize_char = sizeof(char);
swap((void *)&arr_int, sizeof(arr_int), 1, 2, elementSize_int);
swap((void *)&arr_flt, sizeof(arr_flt), 1, 2, elementSize_flt);
swap((void *)arr_char, elementSize_char, 1, 2, elementSize_char);

printf("After swapping:\n");
printf("arr_int: ");
for (size_t i = 0; i < sizeof(arr_int) / sizeof(arr_int[0]); ++i) {
printf("%d ", arr_int[i]);
}
printf("\n");

printf("arr_flt: ");
for (size_t i = 0; i < sizeof(arr_flt) / sizeof(arr_flt[0]); ++i) {
printf("%f ", arr_flt[i]);
}
printf("\n");

printf("arr_char: %s\n", arr_char);

return 0;
}

Common Mistakes

  1. Forgetting to cast void pointers when assigning or dereferencing: Always remember to cast a void pointer back to the appropriate data type before using it.
  1. Not freeing temporary memory allocated with malloc() in functions with void pointers: If you allocate memory temporarily within a function, don't forget to free it when you're done.
  1. Passing the wrong size argument to swap(): Make sure you pass the correct size of the variables being swapped as an argument to the swap() function.
  1. Not handling array bounds correctly: Always check for array boundaries before accessing elements to avoid segmentation faults.

Practice Questions

  1. Write a function that takes two void pointers and returns their sum if they point to integers, their concatenation if they point to strings, and prints an error message otherwise.
  1. Implement a generic bubble sort algorithm using void pointers and the swap() function from the worked example.
  1. Write a function that takes a void pointer to a struct and returns the sum of all its numeric fields (int, float, double).

FAQ

  1. Why can't I directly compare two void pointers? Because void pointers don't have a specific data type, they cannot be compared directly. You should cast them to appropriate data types before comparing.
  1. Can I use void pointers for function arguments in the main() function? Yes, you can pass function arguments as void pointers from the main() function and then cast them back to their original data type within the function.
  1. What happens if I assign a void pointer to a pointer of an incorrect data type? If you try to assign a void pointer to a pointer of an incorrect data type without casting it, your program may behave unexpectedly or crash due to type mismatches.
  1. Can I use void pointers with multidimensional arrays? Yes, you can use void pointers with multidimensional arrays by first converting the multi-dimensional array into a one-dimensional array using pointer arithmetic.