将函数指针作为参数传递给函数??? [英] Passing function pointer as argument to a function???
问题描述
库函数''qsort''因此声明:
void qsort(void * base,size_t nmemb,size_t size,
int(* compar)( const void *,const void *));
如果在我的代码中我写道:
int cmp_fcn(...);
int(* fcmp)()=& cmp_fcn;
qsort(...,fcmp);
然后一切正常。但是如果我将qsort编码为:
qsort(...,& cmp_fcn);
编译器抱怨指针类型不兼容。
有人可以帮我理解我正在传递的内容
坏中的qsort函数除指针之外的代码
到一个函数和/或它与good的区别。代码?
如果没有
直接写qsort语句如何使用像''fcmp'这样的中介?
感谢您的帮助。
问候,
Charles Sullivan
Charles Sullivan在09/19/05 11:05写道:库函数''qsort''因此声明:
void qsort(void * base,size_t nmemb,size_t size,
int(* compar)(const void *,const void *));
如果在我的代码中我写道:
int cmp_fcn(...);
int(* fcmp)()=& cmp_fcn;
FYI:'&''是无害的,但是没有必要,这里都是
及以下。
qsort(.. 。,fcmp);
然后一切正常。但是,如果我将qsort编码为:
qsort(...,& cmp_fcn);
编译器抱怨指针类型不兼容。
>有人可以帮我理解我正在传递的内容吗?糟糕中的qsort功能除指针之外的代码功能和/或它与好的不同之处码?
嗯,让我们看看:编译器说cmp_fcn()是错误类型的
。这可能意味着cmp_fcn()有一些错误吗?
错了?这似乎是一种可能性,不是吗?
然而,这只是我的猜测,因为你没有(暗示,提示)显示
什么cmp_fcn()看起来像...
如果不使用像''fcmp'这样的中介来直接编写qsort语句?
通过编写与qsort()
需要匹配的cmp_fcn()。
-
Er ********* @ sun.com
Charles Sullivan写道:
库函数''qsort''因此声明:
void qsort(void * base,size_t nmemb, size_t size,
int(* compar)(const void *,const void *));
如果在我的代码中我写:
int cmp_fcn(...);
int(* fcmp)()=& cmp_fcn;
qsort(...,fcmp);
然后一切正常。但是,如果我将qsort编码为:
qsort(...,& cmp_fcn);
编译器抱怨指针类型不兼容。
qsort期望指向一个返回int的函数的指针,并且
传递两个const void *参数。但是,你正在传递
它是一个指向函数的指针,该函数返回int并传递一个
可变数量的未知参数。
可以某人帮助我准确理解我正在传递的内容
坏中的qsort函数除指针之外的代码功能和/或它与好的不同之处码?
当使用中间fcmp变量时,你传递它
a函数返回int,其参数保持不变
unprototyped 。
如果不使用像''fcmp'这样的中介,qsort语句怎么能直接写?
实际上,如果cmp_fcn()确实采用两个const void *参数,那么
通过改变来表示:
int cmp_fcn(...);
到
int cmp_fcn(const void *,const void *);
-
+ ------------------------- + -------------------- + - ---------------------------- +
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody / at\spamcop.net | www.fptech.com | #include< std_disclaimer.h> |
+ ------------------------- + -------------- ------ + ----------------------------- +
不要给我发电子邮件:< mailto:Th ************* @ gmail.com>
Charles Sullivan写道:因此声明了库函数''qsort'':
void qsort(void * base,size_t nmemb,size_t size,
int(* compar)(const void) *,const void *));
如果在我的代码中我写:
int cmp_fcn(...);
int(* fcmp)()=& cmp_fcn ;
qsort(...,fcmp);
然后一切正常。但是,如果我将qsort编码为:
qsort(...,& cmp_fcn);
编译器抱怨指针类型不兼容。
>有人可以帮我理解我正在传递的内容吗?糟糕中的qsort功能除指针之外的代码功能和/或它与好的不同之处代码?
如果不使用像''fcmp'这样的中介来直接编写qsort语句?
正如你使用fcmp一样。澄清:
#include< stdio.h>
#include< time.h>
#include< ; stdlib.h>
#include< string.h>
#define ASIZE 10
int cmp_fcn(const void * e1,const void * e2);
int main(void)
{
double asrc [ ASIZE],awrk [ASIZE];
int(* fcmp)()= cmp_fcn;
size_t i;
srand(time(0 ));
printf("使用cmp_fcn作为参数的例子\ n");
for(i = 0; i< ASIZE; i ++)
asrc [i] = rand()/(1. + RAND_MAX);
memcpy(awrk,asrc,sizeof asrc);
qsort(awrk) ,ASIZE,sizeof * awrk,cmp_fcn);
for(i = 0; i< ASIZE; i ++)
printf("%lu:%f%f \ n",(unsigned long)i,asrc [i],awrk [i]);
printf(" \ n");
printf("示例使用fcmp作为参数\ n");
for(i = 0; i< ASIZE; i ++)
as rc [i] = rand()/(1。 + RAND_MAX);
memcpy(awrk,asrc,sizeof asrc);
qsort(awrk,ASIZE,sizeof * awrk,fcmp);
for(i = 0; i< ASIZE; i ++)
printf("%lu:%f%f \ n",(unsigned long)i,asrc [i],awrk [i]);
返回0;
}
int cmp_fcn(const void * e1,const void * e2)
{
const double * p1 = e1,* p2 = e2;
return(* p1> * p2)? 1:(* p1< * p2)? -1:0;
}
使用cmp_fcn作为参数的示例
0:0.563498 0.111813
1:0.195851 0.195851
2:0.111813 0.254336
3:0.294201 0.294201
4:0.318893 0.318893
5 :0.586812 0.511567
6:0.724280 0.563498
7:0.925669 0.586812
8:0.511567 0.724280
9:0.254336 0.925669
使用fcmp作为参数的例子
0:0.887919 0.053615
1:0.053615 0.134536
2:0.471209 0.211441
3:0.457827 0.240006
4:0.640444 0.457827
5:0.211441 0.471209
6: 0.240006 0.511690
7:0.511690 0.640444
8:0.699548 0.699548
9:0.134536 0.887919
The library function ''qsort'' is declared thus:
void qsort(void *base, size_t nmemb, size_t size,
int(*compar)(const void *, const void *));
If in my code I write:
int cmp_fcn(...);
int (*fcmp)() = &cmp_fcn;
qsort(..., fcmp);
then everything works. But if instead I code qsort as:
qsort(..., &cmp_fcn);
the compiler complains about incompatible pointer type.
Can someone help me understand exactly what I''m passing to
the qsort function in the "bad" code other than the pointer
to a function and/or how this differs from the "good" code?
How could the qsort statement be written directly without
the use of an intermediary like ''fcmp'' ?
Thanks for your help.
Regards,
Charles Sullivan
Charles Sullivan wrote On 09/19/05 11:05,:The library function ''qsort'' is declared thus:
void qsort(void *base, size_t nmemb, size_t size,
int(*compar)(const void *, const void *));
If in my code I write:
int cmp_fcn(...);
int (*fcmp)() = &cmp_fcn;
FYI: The `&'' is harmless but unnecessary, both here
and below.
qsort(..., fcmp);
then everything works. But if instead I code qsort as:
qsort(..., &cmp_fcn);
the compiler complains about incompatible pointer type.
Can someone help me understand exactly what I''m passing to
the qsort function in the "bad" code other than the pointer
to a function and/or how this differs from the "good" code?
Well, let''s see: The compiler says that cmp_fcn() is
of the wrong type. Might this mean that there''s something
wrong with cmp_fcn()? It seems a possibility, doesn''t it?
However, this is all just speculation on my part, since you
haven''t (hint, hint) shown what cmp_fcn() looks like ...
How could the qsort statement be written directly without
the use of an intermediary like ''fcmp'' ?
By writing a cmp_fcn() that matches what qsort()
requires.
--
Er*********@sun.com
Charles Sullivan wrote:
The library function ''qsort'' is declared thus:
void qsort(void *base, size_t nmemb, size_t size,
int(*compar)(const void *, const void *));
If in my code I write:
int cmp_fcn(...);
int (*fcmp)() = &cmp_fcn;
qsort(..., fcmp);
then everything works. But if instead I code qsort as:
qsort(..., &cmp_fcn);
the compiler complains about incompatible pointer type.
qsort is expecting a pointer to a function which returns an int and
gets passed two const void * parameters. However, you are passing
it a pointer to a function which returns int and gets passed a
variable number of unknown parameters.
Can someone help me understand exactly what I''m passing to
the qsort function in the "bad" code other than the pointer
to a function and/or how this differs from the "good" code?
When using the intermediate fcmp variable, you are passing it
a function which returns int, and whose parameters are left
unprototyped.
How could the qsort statement be written directly without
the use of an intermediary like ''fcmp'' ?
If cmp_fcn() does, in fact, take two const void * parameters, then
say so by changing:
int cmp_fcn(...);
to
int cmp_fcn(const void *,const void *);
--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don''t e-mail me at: <mailto:Th*************@gmail.com>
Charles Sullivan wrote:The library function ''qsort'' is declared thus:
void qsort(void *base, size_t nmemb, size_t size,
int(*compar)(const void *, const void *));
If in my code I write:
int cmp_fcn(...);
int (*fcmp)() = &cmp_fcn;
qsort(..., fcmp);
then everything works. But if instead I code qsort as:
qsort(..., &cmp_fcn);
the compiler complains about incompatible pointer type.
Can someone help me understand exactly what I''m passing to
the qsort function in the "bad" code other than the pointer
to a function and/or how this differs from the "good" code?
How could the qsort statement be written directly without
the use of an intermediary like ''fcmp'' ?
Exactly as you use fcmp. To clarify:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#define ASIZE 10
int cmp_fcn(const void *e1, const void *e2);
int main(void)
{
double asrc[ASIZE], awrk[ASIZE];
int (*fcmp) () = cmp_fcn;
size_t i;
srand(time(0));
printf(" example using cmp_fcn as argument\n");
for (i = 0; i < ASIZE; i++)
asrc[i] = rand() / (1. + RAND_MAX);
memcpy(awrk, asrc, sizeof asrc);
qsort(awrk, ASIZE, sizeof *awrk, cmp_fcn);
for (i = 0; i < ASIZE; i++)
printf("%lu: %f %f\n", (unsigned long) i, asrc[i], awrk[i]);
printf("\n");
printf(" example using fcmp as argument\n");
for (i = 0; i < ASIZE; i++)
asrc[i] = rand() / (1. + RAND_MAX);
memcpy(awrk, asrc, sizeof asrc);
qsort(awrk, ASIZE, sizeof *awrk, fcmp);
for (i = 0; i < ASIZE; i++)
printf("%lu: %f %f\n", (unsigned long) i, asrc[i], awrk[i]);
return 0;
}
int cmp_fcn(const void *e1, const void *e2)
{
const double *p1 = e1, *p2 = e2;
return (*p1 > *p2) ? 1 : (*p1 < *p2) ? -1 : 0;
}
example using cmp_fcn as argument
0: 0.563498 0.111813
1: 0.195851 0.195851
2: 0.111813 0.254336
3: 0.294201 0.294201
4: 0.318893 0.318893
5: 0.586812 0.511567
6: 0.724280 0.563498
7: 0.925669 0.586812
8: 0.511567 0.724280
9: 0.254336 0.925669
example using fcmp as argument
0: 0.887919 0.053615
1: 0.053615 0.134536
2: 0.471209 0.211441
3: 0.457827 0.240006
4: 0.640444 0.457827
5: 0.211441 0.471209
6: 0.240006 0.511690
7: 0.511690 0.640444
8: 0.699548 0.699548
9: 0.134536 0.887919
这篇关于将函数指针作为参数传递给函数???的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!