What will be the output of the following program?
int *call();
void main() {
int *ptr = call();
printf("%d : %u",*ptr,ptr);
clrscr();
printf("%d",*ptr);
}
int *call() {
int x = 25;
++x;
//printf("%d : %u",x,&x);
return &x;
}
Expected Output: Garbage value
Actual Output: 26 #someaddr
Since x is a local variable it’s scope ends within the function call. I found this code as an example for dangling pointer.
the output of this function is undefined. As you already pointed out the scope of x ends with the function. But the memory where 26 has been written is not used agian. So printing this value will give 26. If this memory is used again, it could be anything.