Define Null Pointer?


What is Null Pointer. Define it?


We use pointer in the programming language. It is the programming language data type. It's (pointer) value refers to directly another value which is stored elsewhere in the computer.
A Null pointer is a special pointer which is pointing to nothing. It has reserved value. It indicates the base address of the segment. We use Null pointer in the following ways:-



  • To stop indirection in a recursive data structure.
  • As a sentinel value.
  • As an error value.

Examples:-

  • int *ptr=NULL;
  • char *ptr=’\0’;
  • float *ptr=(float *)0;




  1. #include<stdio.h>
  2. void main()
  3. {
  4.           int a;
  5.           static int count;
  6.           for(a=NULL; a<=5)
  7.           {
  8.                   count++;
  9.                   a+=3;
  10.            }
  11.           printf(“%d”,count);
  12. }


Output:-
  • 4


  1. #include<stdio.h>
  2. void main()
  3. {
  4.           char *str=NULL;
  5.           strcpy(str, "Hello");
  6.           printf(“%s”,str);
  7. }


Output:-
  • Null (We can not copy anything in the Null Pointer).


No comments:

Post a Comment