Void pointer vs NULL pointer | SoloLearn: Learn to code for FREE!


Void pointer vs NULL pointer

What is the difference between void pointer and null pointer in C programming?

1/10/2019 2:15:00 PM

Ashish B

7 Answers

New Answer


'void' is a data type, which means that the memory location the pointer points to has no specific type, however, it can be casted to a data type if required. 'NULL' is not quite a data type, but rather a value. It's a macro that is used to indicate that the pointer currently does not point to a memory location. This also means that a void pointer can be null, and a null pointer can be of type void. Further reference: https://www.quora.com/What-is-difference-between-null-pointer-and-void-pointer-in-C https://stackoverflow.com/questions/4334831/what-is-a-void-pointer-and-what-is-a-null-pointer


A pointer pointing to null is a null pointer, meaning it doesn't point to anything. A void pointer can point to any address without knowing the type that it is pointing to. A void pointer can point to null in which case the void pointer is a null pointer. Because a void pointer does not know what type it is pointing to you cannot dereference it without explicitly casting it.


@Shadow , Thank you!!


@Dennis, Thank you!


how do you initialize a pointer as nul?


Syntax for null pointer initialization .. Syntax: <data type> *<variable name> = NULL; Example: int *ptr = NULL;                  char *ptr = '\0';


Examples of null pointer and void