argument 3 confusion in scandir()

This is a discussion on argument 3 confusion in scandir() within the C Programming forums, part of the General Programming Boards category; Hi, this might be an easy question: I want to use scandir() Code: int scandir(const char *dir, struct dirent ***namelist, ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    9

    argument 3 confusion in scandir()

    Hi,

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

    Code:
    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
    Code:
    int file_select();
    declared.

    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 = &#37;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);
    }
    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);
    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

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,541
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    9

    thanks

    ok thanks,

    so then there was just the const missing. ARGH

    thanks for pointing that out
    TurboToJo

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21