Void pointers: These are general purpose pointers which do not have any type associated with them and can contain the address of any type of variable. So basically, the type of data that it points to can be anything.
- Void pointers cannot be dereferenced unless it is typecasted to some other pointer type.
- Pointer arithmetic is not possible on pointers of void due to lack of concrete value.
printf("%d", *(int*)p); //output: 10
printf("%c", *(char *)p); //output: A
Hence we need not declare pointers for every data type. A single void pointer can be used to store the address of any type of variable and late be dereferenced.
Null pointers: NULL Pointer is a pointer which is points to nothing. In case, if we don’t have the address to be assigned to a pointer, then we can simply use NULL.
*ptr = NULL;
"The value of ptr is %d"
, ptr); //Value of ptr is 0
Check out Dangling pointer here - https://www.faceprep.in/procoder/knowledgebase/dangling-pointer-in-c/