M
mdh
May I ask the following.
By K&R's own admission, the example used to describe function pointers
is complex ( on P119). In addition, the use of casts has been stated
by some on this group as being, again, a poor/bad example of it's use.
For the moment, accepting these criticisms, I would still like to get
some insight into why/how some things work as even poor code is
enlightening, to me at least.
The example uses K&R's version of qsort ( not the standard one) and
the declaration is as follows:
void qsort(void *lineptr[], int left, int right. int (*comp)(void *,
void *));
The example then chooses from one of 2 methods for comparing strings,
whose declaration are:
int numcmp (*char, *char);
int strcmp( *char, *char);
The call to qsort is as follows:
qsort (void**)lineptr, 0, nlines-1, int (*)(void*, void*)) numeric?
numcmp:strcm)); /* numeric is an integer set based on an optional
command line argument) */
One last declaration before my question.
The swap function is declared as:
void swap(void *[], int, int);
Within the qsort function, the call to the function pointer is as
follows:
if ((*comp)(v, v
By K&R's own admission, the example used to describe function pointers
is complex ( on P119). In addition, the use of casts has been stated
by some on this group as being, again, a poor/bad example of it's use.
For the moment, accepting these criticisms, I would still like to get
some insight into why/how some things work as even poor code is
enlightening, to me at least.
The example uses K&R's version of qsort ( not the standard one) and
the declaration is as follows:
void qsort(void *lineptr[], int left, int right. int (*comp)(void *,
void *));
The example then chooses from one of 2 methods for comparing strings,
whose declaration are:
int numcmp (*char, *char);
int strcmp( *char, *char);
The call to qsort is as follows:
qsort (void**)lineptr, 0, nlines-1, int (*)(void*, void*)) numeric?
numcmp:strcm)); /* numeric is an integer set based on an optional
command line argument) */
One last declaration before my question.
The swap function is declared as:
void swap(void *[], int, int);
Within the qsort function, the call to the function pointer is as
follows:
if ((*comp)(v, v
) < 0)
swap (v, ++last, i);
If qsort casts the comparison function to void pointers, as it does in
"main", then why is it ok for the call in qsort to pass as arguments
to void pointers when the functions numcmp/strcmp expect as arguments
2 char pointers? Or..I might be totally confusing the issue, which is
equally likely. Swap seems to act as I would expect, ie it receives as
it's arguments, an array of pointers to void, plus 2 integers.
thanks in advance.
swap (v, ++last, i);
If qsort casts the comparison function to void pointers, as it does in
"main", then why is it ok for the call in qsort to pass as arguments
to void pointers when the functions numcmp/strcmp expect as arguments
2 char pointers? Or..I might be totally confusing the issue, which is
equally likely. Swap seems to act as I would expect, ie it receives as
it's arguments, an array of pointers to void, plus 2 integers.
thanks in advance.