Thread: Trouble understanding function pointers...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Location
    Falls Church, VA
    Posts
    3

    Question Trouble understanding function pointers...

    Hello fellow programmers,

    I was reading up on function pointers in my copy of "The C Programming Language" when I found the below statement (p119).
    Code:
    (int (*)(void*,void*))(numeric ? numcmp : strcmp));
    I think I know what it is trying to do, though I just do not understand the syntax. Both numcmp and strcmp are pointers to different sort functions each having a return type of int and two pointers as arguments. I assume that the lonely * is the indirection operator that will go with whatever function pointer wins out in the conditional statement. Am I tracking or is there something else going on here?

    Also, when using function pointers must I declare the function pointer explicitly? For example if the first time I use a function pointer is as an argument to another function must I have declared the function pointer earlier?

    Example
    Code:
    void PassPtr(int (*pt2Func)(float, char, char))
    {
       int result = (*pt2Func)(12, 'a', 'b');     // call using function pointer
       cout << result << endl;
    }
    Last edited by fitzpatrickbt; 09-14-2009 at 10:19 PM.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Yes -- this expression yields a function pointer to a function taking two void * arguments and which returns an integer. If "numeric" is true, you get a pointer to numcmp(), else a pointer to strcmp().
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Sep 2009
    Location
    Falls Church, VA
    Posts
    3
    brewbuck,

    Thanks for the quick response. I can now ask my next function pointer question. In the same program I mentioned above there is a function called qsort. The declaration is below:

    Code:
    void qsort(void *lineptr[], int left, int right,
          int(*comp)(void *, void *));
    Here the function takes an array of pointers, two integers and a function pointer. Later in the program the qsort function is called as shown below:

    Code:
    qsort((void **) lineptr, 0, nlines-1,
          (int (*)(void*,void*))(numeric ? numcmp : strcmp));
    Exactly what is going on with the (void**) part?

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by fitzpatrickbt View Post
    Exactly what is going on with the (void**) part?
    I have no idea, as that is not even remotely the correct way to call qsort().
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    Sep 2009
    Location
    Falls Church, VA
    Posts
    3
    My only guess is that they are trying to do two things simultaneously. First, use a pointer-to-pointer on lineptr. Second, to cast the pointer to void. I did find an article on the pointer-to-pointer being necessary to pass the target of a pointer (not just pass the pointer by value) to a function, though they did it in the prototype. I copied the exact syntax out of K&R so I imagine it works. It is definitely confusing though. Maybe I will crank out a test program tomorrow and see what my output is if I remove a *.

    If anyone has another explanation please feel free to explain.

    Here is the URL for the article I mentioned.
    Pointer-to-Pointer and Reference-to-Pointer &mdash; CodeGuru.com

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by fitzpatrickbt View Post
    My only guess is that they are trying to do two things simultaneously. First, use a pointer-to-pointer on lineptr. Second, to cast the pointer to void. I did find an article on the pointer-to-pointer being necessary to pass the target of a pointer (not just pass the pointer by value) to a function, though they did it in the prototype. I copied the exact syntax out of K&R so I imagine it works. It is definitely confusing though. Maybe I will crank out a test program tomorrow and see what my output is if I remove a *.

    If anyone has another explanation please feel free to explain.

    Here is the URL for the article I mentioned.
    Pointer-to-Pointer and Reference-to-Pointer &mdash; CodeGuru.com
    There are two big problems with the code snippet you posted. First, the first argument to qsort() is a void *, not a void **. Casting to void ** will compile, because any pointer can silently transform to void *, but it is an indicator of incorrect thinking.

    Second, the second argument to qsort() is the number of elements to be sorted. It makes no sense for this value to be zero -- qsort() will do nothing at all.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

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. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM

Tags for this Thread