c语言关于函数指针里面用void*传参数的问题

遇女心静 2014-08-13 09:46:35
(1)我定义这样一个函数void insertionSort(int (*comp)(void*,void*));
(2)写了一个函数
int intGreater(int *x,int *y){
return *x-*y;
}
(3)调用的时候insertionSort(intGreater);
会报错:“insertionSort”: 不能将参数 1 从“int (__cdecl *)(int *,int *)”转换为“int (__cdecl *)(void *,void *)”
...全文
788 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2014-08-13
  • 打赏
  • 举报
回复
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
遇女心静 2014-08-13
  • 打赏
  • 举报
回复
引用 4 楼 ParsifalC 的回复:
int (*comp)(void*,void*) int intGreater(int *x,int *y) 这是两种不同的函数指针类型。 一楼说的方法是正确的做法。 在具体的回调函数内部进行类型转换,这样才能发挥回调函数的优势。 在不更改调用方的代码情况下,实现不同的功能。
嗯,一楼方法是可以的,但是就是我要有DOUBLE类型比较,方法的参数都是void*,只能再函数体里面转换,4楼的方法比较好
遇女心静 2014-08-13
  • 打赏
  • 举报
回复
引用 3 楼 lovesmiles 的回复:
typedef int (*func)(void*,void*) insertionSort((func)intGreater);//强制类型转换
谢谢大神,应该强转一下,测试是没问题的。
ParsifalC 2014-08-13
  • 打赏
  • 举报
回复
int (*comp)(void*,void*) int intGreater(int *x,int *y) 这是两种不同的函数指针类型。 一楼说的方法是正确的做法。 在具体的回调函数内部进行类型转换,这样才能发挥回调函数的优势。 在不更改调用方的代码情况下,实现不同的功能。
勤奋的小游侠 2014-08-13
  • 打赏
  • 举报
回复
typedef int (*func)(void*,void*) insertionSort((func)intGreater);//强制类型转换
遇女心静 2014-08-13
  • 打赏
  • 举报
回复
引用 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),只能靠函数名区别了,我想传入参数的时候就限定。
常书 2014-08-13
  • 打赏
  • 举报
回复
可以将 int intGreater(int *x,int *y){ return *x-*y; } 定义为 int intGreater(void *x,void *y){ return *(int*)x-*(int*)y; }
1. C 语言中的指针和内存泄漏 5 2. C语言难点分析整理 10 3. C语言难点 18 4. C/C++实现冒泡排序算法 32 5. C++中指针和引用的区别 35 6. const char*, char const*, char*const的区别 36 7. C中可变参实现 38 8. C程序内存中组成部分 41 9. C编程拾粹 42 10. C语言中实现组的动态增长 44 11. C语言中的位运算 46 12. 浮点的存储格式: 50 13. 位域 58 14. C语言二维组传递方法 64 15. C语言复杂表达式的执行步骤 66 16. C语言字符串函大全 68 17. C语言宏定义技巧 89 18. C语言实现动态组 100 19. C语言笔试-运算符和表达式 104 20. C语言编程准则之稳定篇 107 21. C语言编程常见问题分析 108 22. C语言编程易犯毛病集合 112 23. C语言缺陷与陷阱(笔记) 119 24. C语言防止缓冲区溢出方法 126 25. C语言高效编程秘籍 128 26. C运算符优先级口诀 133 27. do/while(0)的妙用 134 28. exit()和return()的区别 140 29. exit子程序终止函与return的差别 141 30. extern与static存储空间矛盾 145 31. PC-Lint与C\C++代码质量 147 32. spirntf函使用大全 158 33. 二叉树的据结构 167 34. 位运算应用口诀和实例 170 35. 内存对齐与ANSI C中struct内存布局 173 36. 冒泡和选择排序实现 180 37. 指针组与返回组指针的函 186 38. 右左法则- 复杂指针解析 189 39. 回车和换行的区别 192 40. 堆和堆栈的区别 194 41. 堆和堆栈的区别 198 42. 如何写出专业的C头文件 202 43. 打造最快的Hash表 207 44. 指针与组学习笔记 222 45. 组不是指针 224 46. 标准C中字符串分割的方法 228 47. 汉诺塔源码 231 48. 洗牌算法 234 49. 深入理解C语言指针的奥秘 236 50. 游戏外挂的编写原理 254 51. 程序实例分析-为什么会陷入死循环 258 52. 空指针究竟指向了内存的哪个地方 260 53. 算术表达式的计算 265 54. 结构体对齐的具体含义 269 55. 连连看AI算法 274 56. 连连看寻路算法的思路 283 57. 重新认识:指向函的指针 288 58. 链表的源码 291 59. 高质量的子程序 295 60. 高级C语言程序员测试必过的十六道最佳题目+答案详解 297 61. C语言常见错误 320 62. 超强的指针学习笔记 325 63. 程序员之路──关于代码风格 343 64. 指针、结构体、联合体的安全规范 346 65. C指针讲解 352 66. 关于指向指针的指针 368 67. C/C++ 误区一:void main() 373 68. C/C++ 误区二:fflush(stdin) 376 69. C/C++ 误区三:强制转换 malloc() 的返回值 380 70. C/C++ 误区四:char c = getchar(); 381 71. C/C++ 误区五:检查 new 的返回值 383 72. C 是 C++ 的子集吗? 384 73. C和C++的区别是什么? 387 74. 无条件循环 388 75. 产生随机的方法 389 76. 顺序表及其操作 390 77. 单链表的实现及其操作 391 78. 双向链表 395 79. 程序员据结构笔记 399 80. Hashtable和HashMap的区别 408 81. hash 表学习笔记 410 82. C程序设计常用算法源代码 412 83. C语言有头结点链表的经典实现 419 84. C语言惠通面试题 428 85. C语言常用宏定义 450

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧