If I declare an array inside the main() function and send that array into a function as a parameter, can I add elements to that array that aren't pointers created with malloc?
I understand that static variables created within a function are put on the stack and become unreliable after returning from the function. However, because the array is initialized as a static array within main(), the memory locations within the array should be preserved after returning from the helper function, no?
typedef struct test_t {
int x,y;
} test;
void fillArray(test arr[], int length) {
int i;
for (i=0; i<length; i++) {
arr[i] = (test){i,i*3}
}
return;
}
void main() {
test arr[5];
fillArray(arr, 5);
int i;
for (i=0; i<5; i++) {
printf("(%d,%d)\n", arr[i].x, arr[i].y);
}
}
I expect that this toy example will behave properly because there isn't much going on, but is this technically undefined behaviour or is this safe?