Why do we use Null pointers?
c - Is NULL a pointer? - Stack Overflow
c - What is the difference between a pointer pointing to 0x0 location and a pointer set to NULL? - Software Engineering Stack Exchange
arithmetic on NULL pointer -is it undefined behaviour?
The arithmetic itself is not undefined here. Look up how NULL is defined.
Edit: I stand corrected (see below)
More on reddit.comCan a null pointer be compared with other pointers?
What is the purpose of a null pointer in programming?
What is the difference between a null pointer and an uninitialized pointer?
Videos
If we want to "Nullify" something why dont we create a pointer in a controlled way that points to value 0?
In C, NULL is a macro that expands to a null pointer constant.
7.19p3
The macros are
NULL which expands to an implementation-defined null pointer constant; ...
A null pointer constant is an integer constant expression with the value 0 (
e.g., 0, 1-1, 42*0LL, etc.) or such an expression cast to (void*).
6.3.2.3p3
An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant.66) If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function.
Most common C implementations define NULL to be 0, 0L, or ((void*)0).
So you are correct. NULL need not be a pointer.
(IIRC, C++ doesn't even allow the (void*) cast in NULL, meaning NULL in C++ always has integer type. Because of that and because void* pointers
do not compare with regular pointers so readily in C++, C++>=11 now has a special nullptr keyword.)
NULL itself is not a pointer, it is a macro that can be used to initialize a pointer to the null pointer value of its type. When compared to a pointer, it compares equal if the pointer is a null pointer and unequal if the pointer is a valid pointer to an object of its type.
There is no semantic difference between char *p = 0; and char *p = NULL; but the latter is more explicit and using NULL instead of 0 is more informative in circumstances where the other operand is not obviously a pointer or if comparing to an integer looks like a type mismatch:
FILE *fp = fopen("myfile", "r");
if (fp == NULL) {
/* report the error */
}
Similarly, there is no semantical difference in C between '\0' and 0, they both are int constants. The first is the null byte, the second the null value. Using 0, '\0' and NULL wisely may seem futile but makes code more readable by other programmers and oneself too.
The confusion may come from misspelling or mishearing the null pointer as the NULL pointer. The C Standard was carefully proof read to only use null pointer and refer to NULL only as the macro NULL.
Note however that one the accepted definitions of NULL, #define NULL ((void*)0) makes NULL a null pointer to void.
A point that most of the answers here are not addressing, at least not explicitly, is that a null pointer is a value that exists during execution, and a null pointer constant is a syntactic construct that exists in C source code.
A null pointer constant, as Karlson's answer correctly states, is either an integer constant expression with the value 0 (a simple 0 is the most common example), or such an expression cast to void* (such as (void*)0).
NULL is a macro, defined in <stddef.h> and several other standard headers, that expands to an implementation-defined null pointer constant. The expansion is typically either 0 or ((void*)0) (the outer parentheses are needed to satisfy other language rules).
So a literal 0, when used in a context that requires an expression of pointer type, always evaluates to a null pointer, i.e., a unique pointer value that points to no object. That does not imply anything about the representation of a null pointer. Null pointers are very commonly represented as all-bits-zero, but they can be represented as anything. But even if a null pointer is represented as 0xDEADBEEF, 0 or (void*)0 is still a null pointer constant.
This answer to the question on stackoverflow covers this well.
This implies, among other things, that memset() or calloc(), which can set a region of memory to all-bits-zero, will not necessarily set any pointers in that region to null pointers. They're likely to do so on most implementations, perhaps even all existing ones, but the language doesn't guarantee it.
This question is really a duplicate of this one, but Stack Exchange doesn't allow marking duplicates across sites.
On most CPU architectures, which most likely includes whatever CPU architecture you are working on, a pointer pointing to 0x0000 is the exact same thing as a pointer set to NULL.
HOWEVER:
The C standard allows every platform out there to define the internal representation of
NULLas it pleases.The C standard says that if you assign zero to a pointer it will be converted to a
NULLvalue for that platform, but if you take aNULLpointer and cast it toint, there are no guarantees that you will get zero back on every platform out there.
So, some hypothetical AcmePC CPU architecture might find it particularly convenient to represent NULL internally as 0x0badf00d, so assigning zero to a pointer will make it point to address 0x0badf00d, and casting that pointer back to int may yield 0x0badf00d.
I do not know of any architecture that works in such a bizarre way, but then again, the standard allows it, and I do not know every single CPU architecture in existence. So:
If you do not care at all about portability, then take
NULL == 0for granted.If you care a little bit about portability, but are willing to leave out some systems which you have never heard of, then, again, go ahead and take
NULL == 0for granted.But if you really care for portability, then go by what the standard says and only by what the standard says, and consider
NULLas something completely unrelated to0.
For more information you can look at The C Language Specification.