C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-14-2009, 10:13 PM   #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.
fitzpatrickbt is offline   Reply With Quote
Old 09-14-2009, 10:35 PM   #2
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,381
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().
__________________
"Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot
brewbuck is offline   Reply With Quote
Old 09-14-2009, 10:52 PM   #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?
fitzpatrickbt is offline   Reply With Quote
Old 09-14-2009, 11:22 PM   #4
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,381
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().
__________________
"Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot
brewbuck is offline   Reply With Quote
Old 09-14-2009, 11:36 PM   #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
fitzpatrickbt is offline   Reply With Quote
Old 09-14-2009, 11:56 PM   #6
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,381
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.
__________________
"Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot
brewbuck is offline   Reply With Quote
Reply

Tags
conditional operator, function pointers, functions, pointers

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
sorting number Leslie C Programming 8 05-20-2009 04:23 AM
Problem with Visual C++ Object-Oriented Programming Book. GameGenie C++ Programming 9 08-29-2005 11:21 PM
Request for comments Prelude A Brief History of Cprogramming.com 15 01-02-2004 10:33 AM
I need help with passing pointers in function calls vien_mti C Programming 3 04-24-2002 10:00 AM
qt help Unregistered Linux Programming 1 04-20-2002 09:51 AM


All times are GMT -6. The time now is 03:01 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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