I read the concept that variables are only alive for the time execution of the program. Once the functions finish it's job the variables are destroyed. If it is indeed like that then why is it that code like example below works?
Code:
#include <stdio.h>
#include <stdlib.h>
int *find_largest(int a[], int n)
{
int i, *max;
max=&a[0];
for(i=1;i<n;i++)
{
if(a[i]>*max)
{
*max=a[i];
}
}
return max;
}
int main()
{
int *ptr;
int V[]={9, 18, 31, 40, 42};
ptr = find_largest(V, 5);
printf("Largest = %d\n",*ptr);
}