#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    48
    Rep Power
    2

    How to pass a strange argument


    I'm trying to pass glob an argument but I'm not sure how. YES, I've read the docs no help there.
    Here's the argument I'm suppost to pass. What is it?
    It looks like a pointer to something.
    I've copied it right out of the manual so it must be a valid argument to glob.

    int (*errfunc) (const char *filename, int error-code),
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    > int (*errfunc) (const char *filename, int error-code),
    It's a pointer to a function.

    Specifically, a function which takes a char pointer and and int as parameters, and returns an int.

    Say for example
    Code:
    int foo ( const char *filename, int errorcode ) {
        // do some stuff
        return 0;  // Read the glob manual to find out how it uses this result
    }
    To call your glob function with a function pointer, simply do
    glob( foo );
    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
  4. #3
  5. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,111
    Rep Power
    1803
    It is a pointer to a function. You define the function and pass a pointer to it so that the already defined function can call it.

    In this case the function you must supply must habe teh signature:
    Code:
    int userCallbackFunctionName( const char*, int )
    and you simply pass the function pointer using the function name:
    Code:
    glob( userCallbackFunctionName ) ;

    For more than you probably wanted to know about function pointers see http://www.newty.de/fpt/index.html
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    48
    Rep Power
    2
    Originally Posted by salem
    > int (*errfunc) (const char *filename, int error-code),
    It's a pointer to a function.

    Specifically, a function which takes a char pointer and and int as parameters, and returns an int.

    Say for example
    Code:
    int foo ( const char *filename, int errorcode ) {
        // do some stuff
        return 0;  // Read the glob manual to find out how it uses this result
    }
    To call your glob function with a function pointer, simply do
    glob( foo );
    That's great but how do I use it?
    Like if I call glob I have to pass the function which is called what? And do I need to pass the function arguments too?
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,172
    Rep Power
    2222
    errfunc is a function pointer and that line is the declaration for a function pointer which takes two arguments, a C-style string and an int, and returns an int.

    So then, declare and define a function that matches that parameter list and return type. Then in calling the function that asks for that function pointer as an argument, pass it that function name. You'll need to Google on function pointers to find how to also pass the filename and error_code (you shouldn't use a hyphen there, since error-code would parse as one identifier minus a second).

    Similar examples include the quick-sort library function, qsort:
    Code:
    void	qsort	(const void*, size_t, size_t,
                     int (*)(const void*, const void*));
    As you can see, the last parameter is a function pointer. Here's an example of using qsort:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int values[] = { 88, 56, 100, 2, 25 };
    
    int cmpfunc (const void * a, const void * b)
    {
       return ( *(int*)a - *(int*)b );
    }
    
    int main()
    {
       int n;
    
       printf("Before sorting the list is: \n");
       for( n = 0 ; n < 5; n++ ) {
          printf("%d ", values[n]);
       }
    
       qsort(values, 5, sizeof(int), cmpfunc);
    
       printf("\nAfter sorting the list is: \n");
       for( n = 0 ; n < 5; n++ ) {
          printf("%d ", values[n]);
       }
      
      return(0);
    }
    The a and b parameters associated with the function pointer are supplied by qsort when it uses that comparison function.

    Another example is the Windows multithreading function, beginthread:
    _beginthread (void (*)(void *), unsigned, void*);
    As you can see, the first parameter is a function pointer while the last is a void pointer and is the parameter to be passed to that function being pointed to. An example is:
    Code:
    #include <windows.h>
    #include <process.h>    /* _beginthread, _endthread */
    #include <stddef.h>
    #include <stdlib.h>
    #include <conio.h>
    
    void Bounce( void *ch );
    void CheckKey( void *dummy );
    
    /* GetRandom returns a random integer between min and max. */
    #define GetRandom( min, max ) ((rand() % (int)(((max) + 1) - (min))) + (min))
    
    BOOL repeat = TRUE;     /* Global repeat flag and video variable */
    HANDLE hStdOut;         /* Handle for console window */
    CONSOLE_SCREEN_BUFFER_INFO csbi;    /* Console information structure */
    
    int main()
    {
        CHAR    ch = 'A';
    
        hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
    
        /* Get display screen's text row and column information. */
       GetConsoleScreenBufferInfo( hStdOut, &csbi );
    
        /* Launch CheckKey thread to check for terminating keystroke. */
        _beginthread( CheckKey, 0, NULL );
    
        /* Loop until CheckKey terminates program. */
        while( repeat )
        {
            /* On first loops, launch character threads. */
            _beginthread( Bounce, 0, (void *) (ch++)  );
    
            /* Wait one second between loops. */
            Sleep( 1000L );
        }
    }
    So I would assume that the function that takes that function pointer also asks for filename and error_code.

    I hope that helps.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    48
    Rep Power
    2
    Thanks!

IMN logo majestic logo threadwatch logo seochat tools logo