This blog is under construction

Sunday, 3 November 2013

Null pointers

What is a null pointer?
A pointer that refers to nothing is called null pointer.  A macro named NULL defined in <stdio.h> can be used as null pointer.  The value of NULL is just 0 and it could be assigned to any pointer type.
       int *ptr = NULL;

Difference between null pointer and void pointer?

Void pointer is usually used to hold raw address.  Any pointer of type void is called generic or void pointers.  We cannot directly dereference a void pointer.  We need to type cast the void pointer to some other pointer type for dereferencing.
     int num = 10;
     void *ptr = &num;

Whereas, null pointer is nothing but a pointer that doesn't refers to anything.  And the value of NULL is 0.
     int *ptr = NULL;

How to test for null pointer in c?
Below program explains how to check whether a pointer value is NULL or not.


  #include <stdio.h>
  int main() {
        int *ptr = NULL;

        /* checking whether ptr is NULL or not */
        if (ptr) {
                printf("Pointer ptr refers to some valid address\n");
        } else {
                printf("Pointer ptr is NULL\n");
        }
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Pointer ptr is NULL



Whether null pointer is same as an uninitialized pointer
No, null pointer and uninitialized pointer are not same.  Uninitialized pointer holds some junk value.  Whereas, the value of null pointer is just 0.  Below is the simple program explains the difference between uninitialized and NULL pointers.


  #include <stdio.h>
  int main() {
        int *ptr1, *ptr2 = NULL;

        /* ptr1 is uninitialized */
        if (ptr1) {
                printf("Junk value in pointer ptr1:0x%x\n", (int)ptr1);
        }

        /* ptr2 is initialized */
        if (ptr2 == NULL) {
                printf("ptr2 initialized to NULL\n");
        }
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Junk value in pointer ptr1:0x5eeff4
  ptr2 initialized to NULL



How to return null pointer in c?
Below program explains how to return NULL as return value.


  #include <stdio.h>

  /* gives NUll as return value */
  int * returnNull() {
        return (NULL);
  }

  int main() {
        int *ptr;
        ptr = returnNull();

        /* checking whether the return value from returnNull() is NULL or not */
        if (ptr) {
                printf("Return value from returnNull() is not NULL\n");
        } else {
                printf("Return value from returnNull() is NULL\n");
        }
        return 0;
  }




  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Return value from returnNull() is NULL



What happens when you dereference a null pointer?
Dereferencing a null pointer is not recommended.  If we dereference a null pointer, program execution will terminate with segmentation fault.


  #include <stdio.h>
  int main() {
        int *ptr = NULL;

        /* dereferencing NULL pointer */
        printf("Value of *ptr is %d\n", *ptr);

        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Segmentation fault



What happens if we free a null pointer?
Segmentation fault will occur when we free a null pointer.
     int *ptr = NULL;
     free(ptr);

Common uses of null pointer:

  • Null pointer is used to stop indirection in recursive data structure.  Trees, linked list etc. are few recursive data structure where NULL pointer is used to stop indirection.
  • It is also used as a sentinel value.
          Example: 
        int i;
        char str[3][10] = {"English, Maths, NULL};
        for (i = 0; str[i] !=NULL; i++)   {
                 printf("str[%d]: %s\n", i, str[i]);
        }


No comments:

Post a Comment