I have learned that whenever a function ends, every local variable declared inside of it is erased. That's why we must use malloc when declaring an array.
But when I think about that again when a function ends what is really erased (in case I defined an array) is the pointer to that array and not the array itself, So we could simply solve the former problem by returning the pointer.
Here is a look at my code, why did it work while my professor said "we must use malloc"?
#include <stdio.h>
int * test ()
{
int arr[3]={4,5,6};
return arr;
}
int main() {
printf("Hello, World!\n");
int *arr=test();
printf("%d",arr[1]);
return 0;
}
Edit: I'm working according to the C99 standard if that does make a difference.