![]() |
| | #1 |
| Registered User Join Date: Sep 2009 Location: Falls Church, VA
Posts: 3
| 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)); 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 | |
| | #2 |
| Senior software engineer 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 | |
| | #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 *));
Code: qsort((void **) lineptr, 0, nlines-1,
(int (*)(void*,void*))(numeric ? numcmp : strcmp));
|
| fitzpatrickbt is offline | |
| | #4 |
| Senior software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 5,381
| 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 | |
| | #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 — CodeGuru.com |
| fitzpatrickbt is offline | |
| | #6 | |
| Senior software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 5,381
| Quote:
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 | |
![]() |
| Tags |
| conditional operator, function pointers, functions, pointers |
| Thread Tools | |
| Display Modes | |
|
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 |