Hi,
this might be an easy question: I want to use scandir()
My program simply reads the entries in ".". I know how to get it run when my file_selectCode:int scandir(const char *dir, struct dirent ***namelist, int(*filter)(const struct dirent *), int(*compar)(const struct dirent **, const struct dirent **));
function comes after main() and in main there'sdeclared.Code:int file_select();
My first question is:Code:#include <sys/dir.h> #include <stdio.h> #include <stdlib.h> //exit() #include <string.h> //strcmp() extern int alphasort(); int main() { int file_select(); int count,i; struct dirent **files; count = scandir(".", &files, file_select, alphasort); //count = scandir(".", &files, &file_select, alphasort); if (count <= 0){ printf("No files in this directory\n"); exit(0); } printf("Number of files = %d\n",count); for (i=1;i<count+1;++i) printf("%s\n",files[i-1]->d_name); return 0; } int file_select(struct dirent *entry) { if ((strcmp(entry->d_name, ".") == 0) || (strcmp(entry->d_name, "..") == 0)) return (0); else return (1); }
So why is there no difference if I call scandir() with &file_select or just file_select?
My second issue is:
But I want that the function file_select comes before the main. Then I run into a warning:
warning: passing argument 3 of ‘scandir’ from incompatible pointer type
Ok, I tried it with
count = scandir(".", &files, &file_select, alphasort);
and
count = scandir(".", &files, file_select, alphasort);
and also with this
int file_select() declaration in main() and without...
but it just doesnt work. I guess it has something to do with pointer and implicit declarations. However I have no idea what s the difference.
Thanks TurboToJo



LinkBack URL
About LinkBacks


