Suppose i define the following code:
int *func()
{
int *p=(int *)malloc(sizeof(int)); // memory is allocated from heap
// which can be accessed anytime.
return p; // but we created p which has a local scope and vanishes as
//soon as function exits.
}
Then how does this thing work? p being a local varible (a pointer holding a address to the dynamic memory). I mean the memory itself from HEAP should definitely continue to exist, but the pointer variable has a local scope. How come i will be able to get this pointer?
(*int) func()should beint *func()the same goes for(*int)it should be(int *)as for ur other question, its cause ur returning a copy of the memory address returned frommallocnot&pthat would be returning the address of p which would have being deallocated.