Thursday, January 23, 2014

Pointers: Null Pointer.

C language
C language
Hope you have read the previous post on Dangling pointer ( if not click here.). Here I am going to introduce one more pointers: Null Pointer.

Null Pointer:

The simple meaning of null pointer is, the pointer which is pointing to null value i.e we can say pointer which is pointing to nothing. Null pointer identically points to the base address of memory segment.
Examples of NULL pointer:

1. int *ptr=(char *)0;
2. float *ptr=(float *)0;
3. char *ptr=(char *)0;
4. double *ptr=(double *)0;
5. char *ptr=’\0’;
6. int *ptr=NULL;

We cannot copy any thing in the NULL pointer.

Example:

#include
 <stdio.h>
#include <string.h>
int main(){

char *ptr=NULL;
    strcpy(ptr,
"newtechshow.blogspot.com");
printf("%s",ptrr);


return 0;
}

This program will give output : null (or segment fault).

Application

  • At the end of the linked list, last node's pointer is always null. so that the linked list terminates there otherwise it goes to infinite if last pointer points to garbage values.
You can  think more applications and comment here.

0 comments:

Post a Comment