DAnging Pointer :::::::
If any pointer is pointing the memory address of any variable , but after sometime the variable is deleted while the pointer is still pointing such memory location. Such pointer is known as dangling pointer .
e.g :
#include<stdio.h>
int *call();
void main(){
int *ptr;
ptr=call();
fflush(stdin);
printf("%d",*ptr);
}
int * call(){
int x=25;
++x;
return &x;
}
variable x is local variable. Its scope and lifetime is within the function call hence after returning address of x variable x became dead and pointer is still pointing ptr is still pointing to that location.
If any pointer is pointing the memory address of any variable , but after sometime the variable is deleted while the pointer is still pointing such memory location. Such pointer is known as dangling pointer .
#include<stdio.h>
int *call();
void main(){
int *ptr;
ptr=call();
fflush(stdin);
printf("%d",*ptr);
}
int * call(){
int x=25;
++x;
return &x;
}
Output :::::
Garbage Valuevariable x is local variable. Its scope and lifetime is within the function call hence after returning address of x variable x became dead and pointer is still pointing ptr is still pointing to that location.