C void Pointer
In this page:
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?
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- Dereferencing a
void*directly, when it must be cast first. - Doing pointer arithmetic on a
void*, which is not standard. - Casting to the wrong type and reading garbage.
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: