The code below works correctly and gives the output 4:
int main()
{
int *res = valout();
printf("%d", res[0]);
}
int *valout()
{
static int arr[] = {4, 5, 6};
return arr;
}
The code below returns a segmentation error:
int main()
{
int *res = valout();
printf("%d", res[0]);
}
int *valout()
{
int arr[] = {4, 5, 6};
return arr;
}
Why do we get an error in case 2 and not in case 1? What role does the static keyword play here? Please elaborate on this issue.