功能指针问题P119 K& R. [英] Function pointer question P119 K&R

查看:65
本文介绍了功能指针问题P119 K& R.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以问以下问题。


由K& R自己承认,用于描述函数指针的示例

很复杂(在P119)。此外,该团队的一些人已经说过使用演员阵容

,这又是一个糟糕/糟糕的例子。

那一刻,接受这些批评,我仍然希望得到

一些洞察力为什么/如何工作,因为即使是糟糕的代码也至少对我来说很有启发性。


该示例使用K& R'的qsort版本(不是标准版本)和

声明如下:


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

void *));


然后该示例从2种方法中选择一种来比较字符串,

其声明为:


int numcmp(* char,* char );

int strcmp(* char,* char);


对qsort的调用如下:


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

numcmp:strcm)); / * numeric是一个基于可选的

命令行参数的整数集)* /


在我的问题之前的最后一个声明。


交换函数声明为:


void swap(void * [],int,int);


在qsort函数中,对函数指针的调用如下:


if((* comp)(v [i],v [left ])< 0)

swap(v,++ last,i);


如果qsort将比较函数转换为void指针,就像它一样在

" main"中,为什么qsort中的调用可以作为参数传递

到函数numcmp / strcmp期望作为参数时的void指针

2个指针?或者......我可能完全混淆了这个问题,这个问题同样可能是b $ b。 Swap似乎按照我的预期行事,即它收到了它的参数,一个指向void的指针数组,加上2个整数。


提前谢谢。

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[i], v[left] ) < 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.

推荐答案

mdh< md ** @ comcast.netwrites:


<剪断>
mdh <md**@comcast.netwrites:

<snip>

该示例使用K& R'的qsort版本(不是标准版本)和

声明如下:


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

void *));


然后该示例从两种方法中选择一种来比较字符串,

其声明为:


int numcmp( * char,* char);

int strcmp(* char,* char);


对qsort的调用如下:


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

numcmp:strcm)); / * numeric是一个基于可选的

命令行参数的整数集)* /


在我的问题之前的最后一个声明。


交换函数声明为:


void swap(void * [],int,int);


在qsort函数中,对函数指针的调用如下:


if((* comp)(v [i],v [left ])< 0)

swap(v,++ last,i);
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[i], v[left] ) < 0)
swap (v, ++last, i);



我们现在有了v。你展示的原型有lineptr。我会假设

v是一个无效的行,如lineptr。

We now have v. The prototype you showed had lineptr. I''ll assume the
v is a void ** like lineptr.


如果qsort将比较函数转换为void指针,因为它在

" main"中,为什么qsort中的调用可以作为参数传递

到函数numcmp / strcmp期望作为参数时的void指针

2个指针?
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?



不行(一般情况下)。它在实践中有效,因为void *和

char *必须具有相同的大小和表示但是如果lineptr

数组是一个结构指针数组,它可能非常在一台真机上虽然很失败

(虽然很旧)。


您可以将函数指针转换为任何类型的函数指针

喜欢但是为了安全起见,当你打电话给它时,你已经将它转换回

(一个指针)你实际调用的函数的类型和

你必须传递可接受的参数按照正常的

函数调用规则。我说可以接受因为确切的规则是

相当罗嗦,并不是我们问题的主题。

It is not OK (in general). It works in practise because void * and
char * must have the same size and representation but if the lineptr
array were, say, an array of struct pointers it might very well fail
on a real machine (albeit an old one).

You can convert a function pointer to any type of function pointer you
like but to be safe, when you call it, you have converted it back to
(a pointer to) the type of the function you are actually calling and
you must pass arguments that are "acceptable" as per the normal
function call rules. I say "acceptable" because the exact rules are
rather wordy and are not really the subject of our question.


Or ..我可能完全混淆了问题,同样可能是
。 Swap似乎按照我的预期行事,即它接收的是它的参数,一个指向void的指针数组,加上2个整数。
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.



是的,调用swap是没问题的。


-

Ben。

Yes, the call to swap is fine.

--
Ben.


文章< 87 ************ @ bsb.me.uk>,

Ben Bacarisse< ;是******** @ bsb.me.ukwrote:

In article <87************@bsb.me.uk>,
Ben Bacarisse <be********@bsb.me.ukwrote:


>

我们现在有v。你展示的原型有lineptr。我会假设

v是一个无效的行,如lineptr。
>
We now have v. The prototype you showed had lineptr. I''ll assume the
v is a void ** like lineptr.

如果qsort将比较函数转换为void指针,因为它在

" main"中,为什么qsort中的调用可以作为参数传递

到函数numcmp / strcmp期望作为参数时的void指针

2个指针?
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?



不行(一般情况下)。它在实践中有效,因为void *和

char *必须具有相同的大小和表示但是如果lineptr

数组是一个结构指针数组,它可能非常很糟糕

在一台真机上(虽然是旧机器)。


It is not OK (in general). It works in practise because void * and
char * must have the same size and representation but if the lineptr
array were, say, an array of struct pointers it might very well fail
on a real machine (albeit an old one).



Ben,你说void *和char *必须有相同的大小和

代表。你是说这是C中的一个事实,或者你是否b / b $ b表示程序按预期工作必须如此,并且它是程序员的b $ b确保这一事实成真。

Ben, you say that void * and char * must have the same size and
representation. Are you saying that this is a fact in C, or are you
saying that it must be so for the program to work as intended and it is
up to the programmer to ensure that this fact is true.


m说:


< snip>
m said:

<snip>

Ben,你说void *和char *必须具有相同的大小和

表示。你是说这是C中的事实,
Ben, you say that void * and char * must have the same size and
representation. Are you saying that this is a fact in C,



这是事实上的C.


埋藏在3.1.2.5我们发现指向void的指针应具有相同的

表示和对齐要求作为指向字符

类型的指针。其他指针类型不需要具有相同的表示或

对齐要求。


-

Richard Heathfield< http ://www.cpax.org.uk>

电子邮件:-http:// www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日

It''s a fact in C.

Buried deep in 3.1.2.5 we find that "A pointer to void shall have the same
representation and alignment requirements as a pointer to a character
type. Other pointer types need not have the same representation or
alignment requirements."

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


这篇关于功能指针问题P119 K&amp; R.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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