Your program has undefined behavior: you cannot return an array that has been allocated locally:
int* returnArr()
{
int arr[10];
...
return arr; // <<== This is undefined behavior
}
The results that you see after the call of returnArr
are, well, undefined: the memory returned from the function is up for grabs, so it gets altered by arbitrary events, such as making an addition function call.
But it is actually printing the array elements when the
putchar
function call is commented, garbage values when thegetchar
function call is included in the program code.
Without the call of putchar
the memory of arr
stays undisturbed, so you get old values. When you call putchar
, its return address is placed on the stack, right at the spot where arr
is stored. This is a general problem with undefined behavior - you do not get a "fail fast" behavior, or even a guaranteed failure with it. This answer provides a very nice analogy to what's happening when you return a pointer to local from a function.
To fix this problem, allocate the array dynamically, and free it in the caller, like this:
int *arr = malloc(10*sizeof(int)); // Instead of int arr[10];
...
int* arr = returnArr(); // Call the function
... // Use arr here, then...
free(arr); // free it when you are done.