As everyone mentioned already, you are trying to return the address of a temporary variable. What nobody has mentioned is the easiest solution. Cast the variable as static inside the function. This makes it remain in memory for the entirety of the program.
int * func(int *xp)
{
static int y;
y = 10 + *xp;
return (&y);
}
void main()
{
int x = 10;
int *xp = func(&x);
printf("%d\n", x);
printf("%d\n", *xp);
}
This works as expected. Of course there are better ways to go about this, but this is the simplest solution if you indeed want to declare a variable inside the scope of a function, and return it's address. Keep in mind, every time this function is called, it modifies y, it doesn't give you a copy.