/******************************************************************************
Online C Compiler.
Code, Compile, Run and Debug C program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <stdio.h>
// You get a warning for returning the address of a local variable if you do this
/*int* abc (int x) {
int y = x + 1;
return &y;
}*/
// However if you wrap the local's address through another declared pointer variable, it "works"
int* abc (int x) {
int y = x + 1;
int *yy = &y;
return yy;
}
int main()
{
int x = 1;
int* y = abc(x);
/*printf("Return pointer value: %d\n", y);*/
// Uncomment the line above and the line below will print: "Pointer value: 32596"
printf("Pointer value: %d\n", *y); // Otherwise you get "Pointer value: 2", "as expected"
return 0;
}