pointer_to_function? [英] pointer_to_function?

查看:76
本文介绍了pointer_to_function?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮我理解K& R的第119页吗?

这是qsort的一个版本,取决于命令行可选

参数(-n)按字母顺序对行进行排序和字典顺序排序。


我认为相关的行(好吧,我遇到问题

)是如下:

#include .....



void qsort(void * lineptr [],int left,int right,int (* comp)(void *,

void *)); / **声明** /


主要...... {

.....

if( qsort((void **)lineptr,0,nlines-1,(int(*)(void *,void *))

(数字?numcmp:strcmp));

........

}


我看到qsort的decalartion说qsort是

函数,其参数是指针数组(类型为void),2

整数,以及指向函数的指针(接收2 ptrs为

返回一个整数的void类型的参数。


我想我理解void ptrs的相关性(来自我所拥有的
读取)。但是,在主要电话中调用qsort让我感到困惑。我已经看了很多日子,一直在查看常见问题解答等等,所以它是时间到了请求帮助。

解决方案

mdh写道:


可能有人请帮助我理解K& R的第119页?


T.他是qsort的一个版本,取决于命令行可选

参数(-n)按字母顺序对行进行排序与​​字典顺序排序。


我认为相关行(好吧,我遇到问题

)如下:


#include .....



void qsort(void * lineptr [],int left,int right,int(* comp)(void *,

void *)); / **声明** /


主要...... {

.....

if( qsort((void **)lineptr,0,nlines-1,(int(*)(void *,void *))

(数字?numcmp:strcmp));

........

}



int(*)(void *,void *))(数字?numcmp:strcmp)

将(numeric?numcmp:strcmp)的结果转换为所需的

函数指针类型。我假设numcmp使用int *而strcmp使用const

char *作为参数,因此需要演员。

-

Ian柯林斯。


4月30日下午5:46,Ian Collins< ian-n ... @ hotmail.comwrote:


> int(*)(void *,void *))(数字?numcmp:strcmp)


正在铸造(numeric?numcmp:strcmp)的结果为所需的

函数指针类型。我假设numcmp使用int *而strcmp使用const

char *作为参数,因此需要进行强制转换。



正确。

此外,您可能猜到的数字是一个整数变量。

所以,一旦条件表达式得到解决,你最终会得到


> int(* numcmp)(void *,void *))





> int(* strcmp)(void *,void *)?



这对我来说有点令人费解......好吧,其中一件事是

困惑!!





mdh写道:


有人可以帮我理解K& R的第119页?

这是qsort的一个版本,取决于命令行可选

参数(-n)对数字排序vs按字典顺序排列。


我认为相关的行(好吧,我遇到问题

)如下:


#include .....



void qsort(void * lineptr [],int left,int right,int(* comp)( void *,

void *)); / **声明** /


主要...... {

....

if(qsort ((void **)lineptr,0,nlines-1,(int(*)(void *,void *))

(数字?numcmp:strcmp));

.......

}


我看到的是qsort的decalartion说qsort是一个

函数,其参数是指针数组(void类型),2

整数,以及指向函数的指针(接收2 ptrs作为

参数类型void)返回一个整数。


我想我理解void ptrs的相关性(来自我所拥有的
读取)。但是,调用在主要的qsort,让我感到困惑。我已经看了很多一天的大部分时间,一直在阅读常见问题解答等等,所以它是时候要求b / b
帮助。



首先,要预防一些可能的混淆:本练习中的qsort()

函数与函数不同

C标准库中的名称不要理会别人

谁告诉你strcmp()可能不是正确的比较

函数与qsort()一起使用 - 它可能不是,但是他们总是想着一个不同的qsort()函数。


现在问题:你显然对最后一个感到困惑

电话中的参数


qsort(...,(数字? numcmp:strcmp));


这是通常称为三元运算符

或?:的示例;接线员:


条件? true_result:false_result


这个表达式首先计算`condition''(在这种情况下是

`cigue'')。如果条件非零(即为真),则表达式的值为true_result(或者在这种情况下,

为numcmp) ',指向名为numcmp的函数的指针。如果

条件为零(false),则值为false_condition,

或者在这种情况下为strcmp。其他计算机语言有类似的构造,但拼写它不那么令人困惑:


如果数字那么numcmp其他strcmp


....是Algol所说的方式。 C'对简洁的热情得到了完全消除'if'',然后将'then'和'else'拼写为

`?''和'':''分别。


结果是这个对qsort()的调用传递为

第四个参数,指向numcmp的指针( )函数或

a指向strcmp()函数的指针,取决于变量`numeric''的值
。围绕

整个第四个参数的括号是不必要的,并且可能只是为了强调而提供的



我希望这个帮助。


并记住:当你毕业时使用真实的qsort(),

几乎可以肯定*不*使用strcmp()作为最终的

参数。我相信这在FAQ中讨论过。


-

Eric Sosman
es ***** @ acm-dot-org.inva 盖子


Could someone please help me understand p 119 of K&R?

This is a version of qsort that, depending on a command line optional
argument ( -n) sorts lines numerically vs lexicographically.

I think the relevant lines ( well, the ones I am having a problem
with) are as follows.
# include.....
etc
void qsort(void *lineptr[], int left, int right, int (*comp)(void *,
void *)); /** declaration**/

main......{
.....
if(qsort((void**) lineptr, 0, nlines-1, (int (*)(void*, void*))
(numeric ? numcmp:strcmp));
........
}

What I see is that the decalartion of qsort says that qsort is a
function, whose arguments are an array of pointers ( of type void), 2
integers, and a pointer to a function( that recieives 2 ptrs as
arguments of type void) that returns an integer.

I think I understand the relevance of the void ptrs( from what I have
read). However, the call to qsort in main, leaves me perplexed. I have
been looking at this much of the day, been reading FAQs etc, so it''s
time to ask for help.

解决方案

mdh wrote:

Could someone please help me understand p 119 of K&R?

This is a version of qsort that, depending on a command line optional
argument ( -n) sorts lines numerically vs lexicographically.

I think the relevant lines ( well, the ones I am having a problem
with) are as follows.
# include.....
etc
void qsort(void *lineptr[], int left, int right, int (*comp)(void *,
void *)); /** declaration**/

main......{
.....
if(qsort((void**) lineptr, 0, nlines-1, (int (*)(void*, void*))
(numeric ? numcmp:strcmp));
........
}

int (*)(void*, void*))(numeric ? numcmp:strcmp)

Is casting the result of (numeric ? numcmp:strcmp) to the required
function pointer type. I assume numcmp uses int* and strcmp uses const
char* for their parameters, hence the need for a cast.
--
Ian Collins.


On Apr 30, 5:46 pm, Ian Collins <ian-n...@hotmail.comwrote:

>int (*)(void*, void*))(numeric ? numcmp:strcmp)

Is casting the result of (numeric ? numcmp:strcmp) to the required
function pointer type. I assume numcmp uses int* and strcmp uses const
char* for their parameters, hence the need for a cast.


correct.
Also, numeric, as you probably guessed, is an integer variable.
So, once the conditional expression is resolved, do you end up with

>int (* numcmp) (void*, void*))

or

>int (*strcmp)(void*, void*) ?

This is somewhat puzzling to me...well, one of the things that is
puzzing!!





mdh wrote:

Could someone please help me understand p 119 of K&R?

This is a version of qsort that, depending on a command line optional
argument ( -n) sorts lines numerically vs lexicographically.

I think the relevant lines ( well, the ones I am having a problem
with) are as follows.
# include.....
etc
void qsort(void *lineptr[], int left, int right, int (*comp)(void *,
void *)); /** declaration**/

main......{
....
if(qsort((void**) lineptr, 0, nlines-1, (int (*)(void*, void*))
(numeric ? numcmp:strcmp));
.......
}

What I see is that the decalartion of qsort says that qsort is a
function, whose arguments are an array of pointers ( of type void), 2
integers, and a pointer to a function( that recieives 2 ptrs as
arguments of type void) that returns an integer.

I think I understand the relevance of the void ptrs( from what I have
read). However, the call to qsort in main, leaves me perplexed. I have
been looking at this much of the day, been reading FAQs etc, so it''s
time to ask for help.

First, to forestall some possible confusion: The qsort()
function in this exercise is not the same as the function of
that name in the C Standard library. Pay no heed to people
who tell you that strcmp() is probably not the right comparison
function to use with qsort() -- it probably isn''t, but they''re
thinking of a different qsort() function altogether.

Now to the question: You''re apparently confused by the last
argument in the call

qsort (..., (numeric ? numcmp:strcmp));

This is an example of what''s often called the "ternary operator"
or the "?:" operator:

condition ? true_result : false_result

This expression first evaluates `condition'' (in this case
`numeric''). If the condition is non-zero (i.e., true), the
value of the expression is `true_result'' (or in this case,
it is `numcmp'', a pointer to the function named numcmp). If
the condition is zero (false), the value is `false_condition'',
or in this case `strcmp''. Other computer languages have had
a similar construct, but have spelled it less confusingly:

if numeric then numcmp else strcmp

.... was the way Algol put it. C''s passion for brevity gets
rid of the `if'' altogether, and spells `then'' and `else'' as
`?'' and '':'', respectively.

The upshot is that this call to qsort() passes as its
fourth argument either a pointer to the numcmp() function or
a pointer to the strcmp() function, depending on the value
of the variable `numeric''. The parentheses that surround the
entire fourth argument are unnecessary, and are presumably
present only for emphasis.

I hope this helps.

And remember: When you graduate to using the "real" qsort(),
you should almost certainly *not* use strcmp() as the final
argument. I believe this is discussed in the FAQ.

--
Eric Sosman
es*****@acm-dot-org.invalid


这篇关于pointer_to_function?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆