Understanding the void Pointers
Swipe to show menu
When you first saw void, it referred to functions that return nothing. You canโt create a variable of type void, but you can declare a void* pointer that holds the address of any data type.
If you've worked through the pointer exercises in the section, you'll know that all pointers occupy 8 bytes. A void* pointer is no different.
A void* pointer isn't tied to any specific data type. This flexibility allows you to store the address of any data type in it. However, there's a catch: you can't dereference a void* pointer.
This limitation stems from the same reason you can't declare variables of the void type. Perform an explicit type conversion!
Main.c
1234567891011121314151617181920#include <stdio.h> int main() { char c = 'F'; int i = 100; double d = 3.15; void* pV; pV = &c; printf("%c \n", *((char*)pV)); pV = &i; printf("%d \n", *((int*)pV)); pV = &d; printf("%.2f \n", *((double*)pV)); return 0; }
Congratulations on completing the C programming basics! To advance further, explore topics like macros, sorting algorithms, and data structures. Learning a Linux distribution will also help you grow as a skilled C programmer.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat