C
cerr
Hi There,
I would like to have a dynamically extending array of character arrays
consisting of filenames:
What I got so far is:
{
const char *dir_path="/home/reg/";
char **filelist=NULL;
char filename[1024]={0};
int items=0;
struct dirent *dp;
// enter existing path to directory below
DIR *dir = opendir(dir_path);
while((dp=readdir(dir))!=NULL){
if(strstr(dp->d_name, "test")!=NULL){
sprintf(filename,"%s%s",dir_path,dp->d_name);
printf("%s\n",filename);
filelist=realloc(filelist,sizeof(filelist)+sizeof(filename));
filelist[items++]=&filename;
}
}
}
but i end up getting a seg fault and i'm not exactly sure
why.....anyone?
I a;so get a compiler warning:
test.c:19: warning: assignment from incompatible pointer type
line 19 is where i assign the string pointer to the array: ("filelist
[items++]=&filename;")
Thanks a lot!
Ron
I would like to have a dynamically extending array of character arrays
consisting of filenames:
What I got so far is:
{
const char *dir_path="/home/reg/";
char **filelist=NULL;
char filename[1024]={0};
int items=0;
struct dirent *dp;
// enter existing path to directory below
DIR *dir = opendir(dir_path);
while((dp=readdir(dir))!=NULL){
if(strstr(dp->d_name, "test")!=NULL){
sprintf(filename,"%s%s",dir_path,dp->d_name);
printf("%s\n",filename);
filelist=realloc(filelist,sizeof(filelist)+sizeof(filename));
filelist[items++]=&filename;
}
}
}
but i end up getting a seg fault and i'm not exactly sure
why.....anyone?
I a;so get a compiler warning:
test.c:19: warning: assignment from incompatible pointer type
line 19 is where i assign the string pointer to the array: ("filelist
[items++]=&filename;")
Thanks a lot!
Ron