int * addition(int arr[])
{
int sum=0;
for(int i=0;i<4;i++)
sum+=arr[i];
return ∑
}
int main()
{
int arr[4]{1,3,4,5}, * ptr=addition(arr);
cout<<*ptr<<endl;
return 0;
}
As the variable sum
is local to the function addition
so the variable should be destroyed as soon as the program control moves out of the function but it is still giving the output 13
. Why?
Compiler: g++ 4.8.2 on Ubuntu 14.04 LTS