> 
> **Possible Duplicate:**  
> 
> [Returning the address of local or temporary variable](https://stackoverflow.com/questions/2744264/returning-the-address-of-local-or-temporary-variable) 
> 
> 
> 

The add function is implemented wrongly. It should return a value instead of a pointer.
Why aren't any errors when ans and \*ans\_ptr are printed and the program even gives correct result? I guess the variable of z is already out of scope and there should be segmentation fault. 

```
#include <stdio.h>

int * add(int x, int y) {
    int z = x + y;
    int *ans_ptr = &z;
    return ans_ptr;
}

int main() {
    int ans = *(add(1, 2));
    int *ans_ptr = add(1, 2);

    printf("%d\n", *ans_ptr);
    printf("%d\n", ans);

    return 0;
}

```