 Pointer variable point to “nowhere” or “nothing”
 Not to an existing object
 Initialized with a value NULL or 0 regardless of the pointer type
 By default, C doesn’t initialize pointers to NULL
 By default, C doesn’t initialize pointers to NULL
int *p = 0;
int *p = NULL;
 Any pointer which is initialized to 0 or NULL is called a null pointer
 An uninitialized pointer is not a null pointer because it points “somewhere,”
syntax for
declaring a
NULL pointer
 Many functions that return a pointer also need
to indicate an error condition when it occurs
 To pass a null pointer to a function argument
when we don’t want to pass any valid memory
when we don’t want to pass any valid memory
address.
int fun(int *ptr)
{
//……
return 10;
}
fun(NULL);
 To check for null pointer before accessing any
pointer variable.
 Dereferencing a null pointer will abort a program
 To initialize a pointer variable when that pointer
 To initialize a pointer variable when that pointer
variable isn’t assigned any valid memory address yet.
int *p = NULL;
...
if (p != NULL)
printf(“%dn”, *p);
Initialize p to NULL
After processing ...
... check whether p is still null
10. NULL pointer

10. NULL pointer

  • 2.
     Pointer variablepoint to “nowhere” or “nothing”  Not to an existing object  Initialized with a value NULL or 0 regardless of the pointer type  By default, C doesn’t initialize pointers to NULL  By default, C doesn’t initialize pointers to NULL int *p = 0; int *p = NULL;  Any pointer which is initialized to 0 or NULL is called a null pointer  An uninitialized pointer is not a null pointer because it points “somewhere,” syntax for declaring a NULL pointer
  • 4.
     Many functionsthat return a pointer also need to indicate an error condition when it occurs  To pass a null pointer to a function argument when we don’t want to pass any valid memory when we don’t want to pass any valid memory address. int fun(int *ptr) { //…… return 10; } fun(NULL);
  • 5.
     To checkfor null pointer before accessing any pointer variable.  Dereferencing a null pointer will abort a program  To initialize a pointer variable when that pointer  To initialize a pointer variable when that pointer variable isn’t assigned any valid memory address yet. int *p = NULL; ... if (p != NULL) printf(“%dn”, *p); Initialize p to NULL After processing ... ... check whether p is still null