My code is as follows:
#include "stdio.h"
#include "string.h"
struct ABC {
char name[20];
int n;
};
struct ABC& myfun(void) {
struct ABC
x ={ "Lining",99 };
return x;
}
int main(void) {
struct ABC y = myfun();
printf("%s %d\n", y.name, y.n);
return 0;
}
Here I called myfun()
, which returns a reference to a local struct ABC
variable. This should be wrong because after myfun()
returns, the memory it used will no longer serve. But this code runs well in VS2015 and print correct information as "Lining 99".
Why can I get this outcome?