user-0 提问: c语言关于函数指针里面用void*传参数的问题

回复:
user: user-1
content: qsort Performs a quick sort.  void qsort( void *base, size_t num, size_t width, int (__cdecl *compare )(const void *elem1, const void *elem2 ) );  Routine Required Header Compatibility  qsort <stdlib.h> and <search.h> ANSI, Win 95, Win NT    For additional compatibility information, see Compatibility in the Introduction.  Libraries  LIBC.LIB Single thread static library, retail version  LIBCMT.LIB Multithread static library, retail version  MSVCRT.LIB Import library for MSVCRT.DLL, retail version    Return Value  None  Parameters  base  Start of target array  num  Array size in elements  width  Element size in bytes  compare  Comparison function  elem1  Pointer to the key for the search  elem2  Pointer to the array element to be compared with the key  Remarks  The qsort function implements a quick-sort algorithm to sort an array of num elements, each of width bytes. The argument base is a pointer to the base of the array to be sorted. qsort overwrites this array with the sorted elements. The argument compare is a pointer to a user-supplied routine that compares two array elements and returns a value specifying their relationship. qsort calls the compare routine one or more times during the sort, passing pointers to two array elements on each call:  compare( (void *) elem1, (void *) elem2 );  The routine must compare the elements, then return one of the following values:  Return Value Description  < 0 elem1 less than elem2  0 elem1 equivalent to elem2  > 0 elem1 greater than elem2    The array is sorted in increasing order, as defined by the comparison function. To sort an array in decreasing order, reverse the sense of “greater than” and “less than” in the comparison function.  Example  /* QSORT.C: This program reads the command-line  * parameters and uses qsort to sort them. It  * then displays the sorted arguments.  */  #include <stdlib.h> #include <string.h> #include <stdio.h>  int compare( const void *arg1, const void *arg2 );  void main( int argc, char **argv ) {    int i;    /* Eliminate argv[0] from sort: */    argv++;    argc--;     /* Sort remaining args using Quicksort algorithm: */    qsort( (void *)argv, (size_t)argc, sizeof( char * ), compare );     /* Output sorted list: */    for( i = 0; i < argc; ++i )       printf( "%s ", argv[i] );    printf( "\n" ); }  int compare( const void *arg1, const void *arg2 ) {    /* Compare all of both strings: */    return _stricmp( * ( char** ) arg1, * ( char** ) arg2 ); }   Output  [C:\code]qsort every good boy deserves favor boy deserves every favor good   Searching and Sorting Routines  See Also   bsearch, _lsearch

user: user-0
content: 引用 4 楼 ParsifalC 的回复:int (*comp)(void*,void*) int intGreater(int *x,int *y)  这是两种不同的函数指针类型。 一楼说的方法是正确的做法。 在具体的回调函数内部进行类型转换，这样才能发挥回调函数的优势。 在不更改调用方的代码情况下，实现不同的功能。  嗯，一楼方法是可以的，但是就是我要有DOUBLE类型比较，方法的参数都是void*，只能再函数体里面转换，4楼的方法比较好

user: user-0
content: 引用 3 楼 lovesmiles 的回复:typedef  int (*func)(void*,void*)  insertionSort((func)intGreater);//强制类型转换  谢谢大神，应该强转一下，测试是没问题的。

user: user-2
content: int (*comp)(void*,void*) int intGreater(int *x,int *y)  这是两种不同的函数指针类型。 一楼说的方法是正确的做法。 在具体的回调函数内部进行类型转换，这样才能发挥回调函数的优势。 在不更改调用方的代码情况下，实现不同的功能。

user: user-3
content: typedef  int (*func)(void*,void*)  insertionSort((func)intGreater);//强制类型转换

user: user-0
content: 引用 1 楼 r_Jimy 的回复:可以将 int intGreater(int *x,int *y){     return *x-*y; } 定义为 int intGreater(void *x,void *y){          return *(int*)x-*(int*)y; } --------------------- 可以是可以，但是比如要要比较double类型的话，函数也是int doubleGreater(void *x,void *y),只能靠函数名区别了，我想传入参数的时候就限定。

user: user-4
content: 可以将 int intGreater(int *x,int *y){     return *x-*y; } 定义为 int intGreater(void *x,void *y){          return *(int*)x-*(int*)y; }