NULL Pointer:
The integer constant zero(0) has different meanings depending upon it's used. In all cases, it is an integer constant with the value 0, it is just described in different ways.
If any pointer is being compared to 0, then this is a check to see if the pointer is a null pointer. This 0 is then referred to as a null pointer constant. The C standard defines that 0 is typecast to (void *) is both a null pointer and a null pointer constant.
The macro NULL is provided in the header file "stddef.h".
Below are the ways to check for a NULL pointer:
C
- NULL is defined to compare equal to a null pointer as:
if(pointer == NULL)
- Below if statement implicitly checks "is not 0", so we reverse that to mean "is 0" as:
if(!pointer)
- Below statement checks if the string pointer is pointing at a null character.
if (!*string_pointer)
- Below statement checks if the string pointer is pointing at a not-null character.
if (*string_pointer)
// C program to print the value of
// '\0' and '0'
#include <stdio.h>
// Driver Code
int main()
{
// Print the value of
// '\0' and '0'
printf("\\0 is %d\n", '\0');
printf("0 is %d\n", '0');
return 0;
}
Output:
\0 is 0 0 is 48