Inactive Tutor answered 09/29/20
the danger is that when get_val terminates,
the local var x goes out of scope and the address
becomes invalid
Bloodshed dev c++ shows 210, which is the expected result,
but that is by LUCK
Xiana Z.
asked 09/29/20#include <stdio.h>
#include <stdlib.h>
int *get_val(int y) {
int x = 200;
x += y;
return &x;
}
int main() {
int *p, y = 10;
p = get_val(y);
printf("%d\n", *p);
exit(0);
}
Inactive Tutor answered 09/29/20
the danger is that when get_val terminates,
the local var x goes out of scope and the address
becomes invalid
Bloodshed dev c++ shows 210, which is the expected result,
but that is by LUCK
Get a free answer to a quick problem.
Most questions answered within 4 hours.
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.