#include<stdio.h>
int main()
{
int i=3;
float f=3.50,*ptr;
float *multy(int ,float);
ptr=multy(i,f); // Address Return
printf("\n%u %f",ptr,*ptr); // Printing
return 0;
}
float * multy(int ii,float jj)
{
float p=ii*jj; // Memory Allocation
printf("%u %f",&p,p);
return (&p);
}
The function stack would look as under:
"Memory Allocation" happens in function multy(..). This memory is named as 'p'. The print inside the multy(..) works fine and prints the address and value of p as 'p' exists in that stack.
When address is returned from multy(..), the control returns to main function. The address returned is stored in 'ptr'. The memory which was allocated inside the stack of multy(..) is not valid to be used outside the scope of multy(..)
So, when "Printing" happens in main(..), the address is illegal (not meant to be used outside scope of multi(..) ), so you get invalid memory access / segmentation fault.