typedef struct
{
char path[MAX_FILENAME*MAX_FOLDERS];
char filename[MAX_FILENAME];
time_t date;
off_t size;
} FILES;
This code works
FILES *fls = (FILES*)malloc(sizeof(FILES));
strcpy(fls[0].filename, "asd");
printf("%s\n", fls[0].filename);
fls = (FILES*)realloc(fls, 2);
strcpy(fls[1].filename, "asdfgh");
printf("%s\n", fls[0].filename);
printf("%s\n", fls[1].filename);
But here:
void allocateFileTree(FILES *tree,int i)
{
if(i==0)
tree = (FILES*)malloc(sizeof(FILES));
else
tree = (FILES*)realloc(tree, sizeof(FILES)*i);
}
in a loop
allocateFileTree(tree, i);
struct stat buff;
stat(entry -> d_name, &buff);
strcpy(tree[i].path, "whatever");//it gives segfault
i++;//this is never executed so realloc isn't the problem (yet)
Why and how can I solve this? What is so different that it crashes?