Like Tree2Likes
  • 2 Post By Salem

help with passing a pointer to a function to another function

This is a discussion on help with passing a pointer to a function to another function within the C Programming forums, part of the General Programming Boards category; how is the pointer to a particular function passed in the following code Code: qsort((void **)lineptr,0,nlines-1, (int(*)(void*,void*))(numeric?numcmp:strcmp) ); is it ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    7

    help with passing a pointer to a function to another function

    how is the pointer to a particular function passed in the following code
    Code:
    qsort((void **)lineptr,0,nlines-1,(int(*)(void*,void*))(numeric?numcmp:strcmp));
    is it the normal way to pass a pointer to a function to another function i.e. by writing the name of the function after what seems to be the declaration of the function?

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    The (int(*)(void*,void*)) looks like a cast that is being provided to shut up the compiler. Numeric probably tests wether the argument is number or not, if it is, the pointer to numcmp is provided, else strcmp.

  3. #3
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    By right, you should have

    Code:
    int str_compare(const void *a,const void *b)
    {
      return strcmp(a,b);
    }
    
    int int_compare(const void *a,const void *b)
    {
       int p = *(const int*)a;
       int q = *(const int*)b;
      if( p > q) {
         return 1;
      } else if( p < q) {
        return -1;
     } else {
       return 0;
    }
    The cast is necessary to shut off compiler warning. since strcmp is int (*)(const char*,const char*) , and qsort() wants int (*)(const void*,const void*)

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    7
    ok. earlier i did not that int(*)(void *,void*) was actually being used for a cast. thankyou.

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Note that normally you don't need to do this with with qsort(). In this case what is provided as an argument is not simply a function pointer but a complete conditional expression. The cast is simply telling the compiler to don't mind this and just trust that you know what you are doing. Without all this you should be able to call qsort() like this, (for an array of ints, length 10).

    Code:
    int comp(const void* a, const void *b) {
        return *((int*)a) - *((int*)b);
    }
    
    int main()
    {
        int vect[10] = {4,2,3,7,1,3,5,2,8,1};
    
        qsort(vect, 10, sizeof(int), comp);
    
        return 0;
    }

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    Just note that Bayint's compare function is a lot better than Subsonics'

    The latter is susceptible to numeric underflow, which would likely lead to problems.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Replies: 9
    Last Post: 12-25-2007, 04:01 AM
  3. Passing pointer into function bug
    By CodeMonkey in forum C++ Programming
    Replies: 4
    Last Post: 04-26-2005, 11:13 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

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