What is the difference between NULL and VOID?

  • Null is actually a value, whereas Void is a data type identifier.
  • A null variable simply indicates an empty value, whereas void is used to identify pointers as having no initial size.

In the context of data structures and programming, “NULL” and “VOID” are two different concepts.

  1. NULL:
    • NULL typically refers to a pointer that doesn’t point to any memory location or object. It’s often used in languages like C, C++, and pointers in other languages to signify a pointer that isn’t currently pointing to anything valid.
    • For example, in C/C++, when you allocate memory dynamically using functions like malloc() or new, if the allocation fails, the function may return NULL to indicate that no memory could be allocated.
  2. VOID:
    • VOID is a data type used to indicate that a function does not return any value. In C/C++ and many other languages, you declare a function’s return type as VOID when the function doesn’t produce any result that needs to be returned.
    • For example, a function declared as void functionName() indicates that it doesn’t return anything.

To summarize:

  • NULL typically deals with pointers and signifies a lack of valid reference or memory allocation.
  • VOID is a data type used in function declarations to denote that the function doesn’t return any value.