I have the following code that tries to malloc a pointer to a struct outside of the function. Everything works fine inside the function, but once it completes, it seems my memory is lost.
Code:
typedef struct {
char name[1024];
} FileList;
void read_files(FileList *filelist){
long filecount = 5;
long i;
filelist = (FileList*)malloc(sizeof(FileList)*filecount);
for(i=0;i<filecount;i++){
sprintf(filelist[i].name,"filename%d",i);
fprintf(stderr,"Filename %d was %s\n",i,filelist[i].name); //works
}
}
int main(int argc,char **argv){
FileList *files;
read_files(files);
fprintf(stderr,"Filename 0 was %s\n",files[0].name); //doesn't work (returns NULL)
return 0;
}
What can I do to fix this?