← Back to C Course | Chapter 7: Pointers | Lesson 8 of 9

C void Pointer

A void pointer can point to any type of data, like a blank label that can be stuck on anything. You must say what type it really is before using what it points to.
Syntax
c
void *pointer_name = &variable_name;

*(data_type *)pointer_name;  // cast before dereferencing

What is a void Pointer?

Because it carries no type information, the compiler can't perform any type-specific operations on it directly, but this also makes it the natural choice for functions like malloc() that need to hand back memory of any type.

Example: What is a void Pointer?

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	void *ptr;
	// Declare x and set it to 5
	int x = 5;
	ptr = &x;
	// Print formatted text to the screen
	printf("%d", *(int*)ptr);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Assigning Addresses to void Pointers

This flexibility is exactly what makes void pointers useful for writing generic containers or functions (like qsort() or memcpy()) that need to work with data of any type without being rewritten for each one.

Example: Assigning Addresses to void Pointers

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare x and set it to 5
	int x = 5;
	// Declare f and set it to 3.5f
	float f = 3.5f;
	void *ptr;
	ptr = &x;
	ptr = &f;
	// Print formatted text to the screen
	printf("%.1f", *(float*)ptr);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Dereferencing a void Pointer

You must cast a void pointer back to a concrete type -- like int *p = (int*)voidPtr -- before you can dereference it, because the compiler needs to know how many bytes to read and how to interpret them.

Example: Dereferencing a void Pointer

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare x and set it to 42
	int x = 42;
	void *voidPtr = &x;
	int *p = (int*)voidPtr;
	// Print formatted text to the screen
	printf("%d", *p);
	// Return 0 to signal that the program finished successfully
	return 0;
}

void Pointers in Functions

This generality comes at the cost of type safety: the function has no way to verify at compile time that the caller passed the type of data it expects, so bugs here typically only surface at runtime.

Example: void Pointers in Functions

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
void printAsInt(void *data) {
	// Print formatted text to the screen
	printf("%d", *(int*)data);
}
// Program execution starts in main()
int main() {
	// Declare x and set it to 7
	int x = 7;
	printAsInt(&x);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Pointer Arithmetic on void Pointers

This restriction exists because the compiler doesn't know the size of whatever void* is pointing to, so it has no way to calculate how far 'one element forward' actually is -- you must cast to a concrete pointer type first.

Example: Pointer Arithmetic on void Pointers

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int arr[3] = {1, 2, 3};
	void *ptr = arr;
	int *typedPtr = (int*)ptr;
	typedPtr++;
	// Print formatted text to the screen
	printf("%d", *typedPtr);
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Dereferencing a void* directly, when it must be cast first.
  2. Doing pointer arithmetic on a void*, which is not standard.
  3. Casting to the wrong type and reading garbage.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.