#include <iostream>
using namespace std;
int *wedgic(int foo)
{
cout<<foo;
return (&foo);
}
int main()
{
int fo=7;
int *blah;
blah=wedgic(fo);
(*blah)+=3;
wedgic(fo);
}
can anyone tell me why the result is 7 7 7
but no 7 7 10??
please help, I guess I dont understand what does Blah=wedgic(fo); do…
thanks
Firstly, your function
wedgicreturns a pointer to its local variablefoo. The pointer becomes invalid immediately after the functionwedgicterminates. Any attempts to use the returned pointer value lead to undefined behavior. In other words, you program produces no meaningful results. The behavior is undefined. Whatever you see there (7 7 7or whatever else) is just an accident with no meaningful explanation.Secondly, your program make only two attempts to output anything. Yet you claim that it somehow outputted three numbers. This is highly unlikely, even for a program with undefined behavior. It is either that your claim about the output is false, or you posted fake code. When you ask question about the results produced by some code, please, post real code.