I use a 2D array of chars that should be written & read by multiple functions in C.
This is my array:
static char array[3][6];
And let's say I have a function 'Function()' that modify this array. If the function is defined in the main there is no problem (the the array is correctly written & then read), but if I want to have my Function in an another file, the array is correctly written but when I return in the main is magically empty! This is my code.
main.c
#include "support.h"
int main(int argc, char *argv[])
{
Function();
unsigned i, j;
for(i = 0; i < 3; i++)
{
for(j = 0; j < 6; j++)
printf("[%c]", array[i][j]);
printf("\n");
}
system("PAUSE");
return 0;
}
support.h
static char array[3][6];
support.c
void Function()
{
char hello[6];
hello[0] = 'H';
hello[1] = 'E';
hello[2] = 'L';
hello[3] = 'L';
hello[4] = 'O';
hello[5] = '\0';
strcpy(array[0], hello);
}
No compilation error nor runtime error. One again, if I try to move everything in main.c all works, if I divide in two files it doesn't (the array is correct as soon as it returns from the Function(), then it is freed), how is it possible?