Home » C Programs » Dangling Pointer in C

Dangling Pointer in C

Dangling Pointers are those pointers that point to a deallocated memory block. The Dangling Pointer Problem error is caused by this circumstance. When a pointer referring to a variable exits its scope or when memory for an object or variable is deallocated, a dangling pointer is created.

What is Dangling Pointer

A pointer is a variable that keeps track of other variables’ memory addresses. The addresses of additional memory elements may also be stored in the pointers. When it comes to dynamic memory allocation, they are quite useful. On the other hand, a dangling pointer leads to a memory address or a non-existent variable. The pointer that is hanging around could lead to a variable or memory address that was perhaps freed.

What is Function Call in C

An essential component of the C programming language is a function call. When calling a function is necessary, it is invoked inside of a program. Only in a program’s main() method is it called by its name. The arguments can be sent to a function that is called from the main() method.

Dangling Pointer in C

#include <stdio.h>
#include <conio.h>
int *number()
{
int a=10;
return &a;
}
int main()
{
int *ptr=number();
printf("%d", *ptr);
return 0;
}   

Output:

Segmentation Fault