Of course it is not working.
At best, the behaviour is undefined, since Arg()
is returning the address of a local variable (arg)
that no longer exists for main()
. main()
uses that returned address when it is not the address of anything that exists as far as your program is concerned.
There is also the incidental problem that int arg[size]
, where size
is not fixed at compile time, is not valid C++. Depending on how exacting your compiler is (some C++ compilers reject constructs that are not valid C++, but others accept extensions like this) your code will not even compile successfully.
To fix the problem, have your function return a std::vector<int>
(vector
is templated container defined in the standard header <vector>
). Then all your function needs to do is add the values to a local vector, which CAN be returned safely by value to the caller.
If you do it right, you won't even need to use a pointer anywhere in your code.