Archived

This topic is now archived and is closed to further replies.

Mustbmistakin

Pointer problem ???

Recommended Posts

Hi, I''m having difficulty''s implementing a new function in my program, I think the problem is in the use of pointers... I was hoping someone out here could help me ... I wrote a function that looks somewhat like this : int RefreshFileList(FileInfoT *theFiles, char *theDir, int (*select)(const struct dirent*), int fileAttr, int dirAttr) { struct dirent **f; int t1,dt,ft; FileInfoT buf; int fc = 0; fc = scandir(theDir, &f, select, alphasort); if ((theFiles = (FileInfoT *)realloc(theFiles, fc*sizeof(FileInfoT))) == NULL) { endwin(); printf( "\nnot enough memory to store directory listing... \n"); exit(1); }; ft = 0; dt = fc-1; for (t1=0;t1d_type == DT_DIR) { sprintf(theFiles[ft].name,"/%s",f[t1]->d_name); sprintf(theFiles[ft].path,"%s%s/",theDir,f[t1]->d_name); theFiles[ft].attr = fileAttr; ft++; } else { sprintf(theFiles[dt].name," %s",f[t1]->d_name); sprintf(theFiles[dt].path,"%s%s",theDir,f[t1]->d_name); theFiles[dt].attr = dirAttr; dt--; } free(f[t1]); } free(f); return(fc); } I cut some parts of the function becouse I did''nt seem to be important to the problem... But the function is designed to read a directory list, reformat and put it in theFiles... The function seems to work, or at least... calling it with fileCount = RefreshFileList(files, currentDir, FileListFileSelect, MFFILEA, MFDIRECTORYA); seems to wordt fine... But the program dies giving me a segmentation fault as soon as I try to access the files array using : files.name or anything like that.. (i < fileCount) I realy can''t figure out what the problem might be ... I hope someone can help me with this one...

Share this post


Link to post
Share on other sites
hmmm.. Looking at my post...

I''m not such a HTML hacker so I just cut and past the source, but that did not seem to work out the way I imagined ...

well, al least the last programming statement should show how I acces files at a certain index and then the name property...

but the squarebracked followd by the letter ''i'' seems to make it go italic...

Share this post


Link to post
Share on other sites
okay... I''ll restate the problem using proper tags then...

This is my function

  
int RefreshFileList(FileInfoT *theFiles, char *theDir, int (*select)(const struct dirent*),
int fileAttr, int dirAttr)
{
struct dirent **f;
int t1,dt,ft;
FileInfoT buf;
int fc = 0;

fc = scandir(theDir, &f, select, alphasort);
if ((theFiles = (FileInfoT *)realloc(theFiles, fc*sizeof(FileInfoT))) == NULL)
{
endwin();
printf("\nnot enough memory to store directory listing... :(\n");
exit(1);
};
ft = 0; dt = fc-1;
for (t1=0;t1<fc;t1++)
{
if (f[t1]->d_type == DT_DIR)
{
sprintf(theFiles[ft].name,"/%s",f[t1]->d_name);
sprintf(theFiles[ft].path,"%s%s/",theDir,f[t1]->d_name);
theFiles[ft].attr = fileAttr;
ft++;
}
else
{
sprintf(theFiles[dt].name," %s",f[t1]->d_name);
sprintf(theFiles[dt].path,"%s%s",theDir,f[t1]->d_name);
theFiles[dt].attr = dirAttr;
dt--;
}
free(f[t1]);
}
free(f);
return(fc);
}


This is the way I call it :

  
fileCount = RefreshFileList(files, currentDir, FileListFileSelect, MFFILEA, MFDIRECTORYA);


And this is the way I eventualy crash with a segmentation error :
  
mvwaddstr(fileListWnd,t1+1-windowTop,1,files[t1].name);


I hope someone will be able to look at this now...

(that the source tags did they''re work !)

Greetings and most sincere appology''s,
MustBMistakin

Share this post


Link to post
Share on other sites