Thread: argument 3 confusion in scandir()

  1. #1
    Registered User
    Join Date
    Jul 2007

    argument 3 confusion in scandir()


    this might be an easy question: I want to use scandir()

    int scandir(const char *dir, struct dirent ***namelist, int(*filter)(const struct dirent *), int(*compar)(const struct dirent **, const struct dirent **));
    My program simply reads the entries in ".". I know how to get it run when my file_select
    function comes after main() and in main there's
    int file_select();

    #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");
       printf("Number of files = &#37;d\n",count);
       for (i=1;i<count+1;++i)
         return 0;
    int file_select(struct dirent *entry)
       if ((strcmp(entry->d_name, ".") == 0) || (strcmp(entry->d_name, "..") == 0))
         return (0);
         return (1);
    My first question is:
    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);
    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

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > int(*filter)(const struct dirent *)
    Which means your function prototype should look like this
    int file_select ( const struct dirent *d );
    to match the definition which follows.

    The same goes for the compare function.

    Also, you don't need the & in C to indicate that a function name is a function pointer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2007


    ok thanks,

    so then there was just the const missing. ARGH

    thanks for pointing that out

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. member as default argument
    By MarkZWEERS in forum C++ Programming
    Replies: 2
    Last Post: 03-23-2009, 08:09 AM
  2. Scandir ()
    By gurvinder in forum C Programming
    Replies: 1
    Last Post: 08-11-2008, 03:19 PM
  3. function passing argument..array ?
    By jochen in forum C Programming
    Replies: 2
    Last Post: 09-30-2007, 11:53 AM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. Nested loop frustration
    By caroundw5h in forum C Programming
    Replies: 14
    Last Post: 03-15-2004, 08:45 PM
Website Security Test