- Void pointers can be assigned to any other pointer type without an explicit cast.
- When using a void pointer, it needs to be explicitly cast to the correct type before dereferencing it.
Example 1: Assigning a void pointer to a char pointer and printing its value:
void* ptr = malloc(sizeof(char));*(char*)ptr = ‘A’;printf(“%c\n”, *(char*)ptr);
- Example 2: Assigning a void pointer to an int pointer and printing its value:
void* ptr = malloc(sizeof(int));*(int*)ptr = 42;printf(“%d\n”, *(int*)ptr);