本文介绍了来自c faq的关于13.9的问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下网址,他们有

跟随


问:现在我正在尝试用qsort对结构数组进行排序。我的
比较函数需要指向结构的指针,但是编译器

抱怨qsort的函数类型错误。我怎么能用
施放函数指针来关闭警告?


答:转换必须在比较函数中,它必须是

声明为接受``通用指针''''(const void *),如上面讨论的

。对于假设的小日期结构


struct mystruct {

int年,月,日;

};


比较函数可能看起来像[脚注]


int mystructcmp(const void * p1,const void * p2)

{

const struct mystruct * sp1 = p1;

const struct mystruct * sp2 = p2;

if(sp1-> year< sp2-> year)返回-1;

else if(sp1-> year sp2-> year)返回1;

else if(sp1->月份< sp2->月份)返回-1;

否则if(sp1-> month sp2-> month)返回1;

else if(sp1 - > day< sp2-> day)返回-1;

else if(sp1-> day sp2-> day)返回1;

else返回0;

}


(从通用指针到struct mystruct指针的转换

发生在初始化sp1 = p1和sp2 = p2;编译器

隐式执行转换,因为p1和p2无效

指针s。)


问题是,为什么不使用类似的东西

const struct mystruct * sp1 =& p1;

const struct mystruct * sp2 =& p2;

Chad

At the following url http://c-faq.com/lib/qsort2.html, they have the
following

Q: Now I''m trying to sort an array of structures with qsort. My
comparison function takes pointers to structures, but the compiler
complains that the function is of the wrong type for qsort. How can I
cast the function pointer to shut off the warning?

A: The conversions must be in the comparison function, which must be
declared as accepting ``generic pointers'''' (const void *) as discussed
in question 13.8 above. For a hypothetical little date structure

struct mystruct {
int year, month, day;
};

the comparison function might look like [footnote]

int mystructcmp(const void *p1, const void *p2)
{
const struct mystruct *sp1 = p1;
const struct mystruct *sp2 = p2;
if(sp1->year < sp2->year) return -1;
else if(sp1->year sp2->year) return 1;
else if(sp1->month < sp2->month) return -1;
else if(sp1->month sp2->month) return 1;
else if(sp1->day < sp2->day) return -1;
else if(sp1->day sp2->day) return 1;
else return 0;
}

(The conversions from generic pointers to struct mystruct pointers
happen in the initializations sp1 = p1 and sp2 = p2; the compiler
performs the conversions implicitly since p1 and p2 are void
pointers.)

The question is, why don''t you use something like

const struct mystruct *sp1 = &p1;
const struct mystruct *sp2 = &p2;
Chad

推荐答案



初始值设定项的类型和值都是错误的。你想指定

指针值,而不是它的地址。


-

Thad

The initializer would have the wrong type and value. You want to assign
the pointer value, not the address of it.

--
Thad




因为:


& p1 ==变量的地址(参数,实际上)p1


p1 ==保存结构地址的变量比较


* p1 ==比较结构


假设p1包含(随机选择的)地址5,并假设p1是

位于(随机选择)位置27的内存中,并且...

由const struct mystruct *替换。如上所示,

以下是真的:


分配结果

.... sp1 =& p1 sp现在持有27(p1的地址)

.... sp1 = p1 sp现在持有5(p1的内容)

.... sp1 = * p1 sp现在保持<在5号位置找到的值>


但是......

因为p1是函数参数,而不是实际变量,我相信试图取其地址会导致未定义的行为。

(除非采取特殊措施使事情以不同的方式发生,

参数通常是在堆栈上传递,这使得他们的

地址要么不可能,要么毫无意义,因为实际上,

他们真的没有地址可以拿走。)


-

Don Bruder - - 如果你的来自:地址不在我的白名单上,

或邮件的主题不包含确切的文字PopperAndShadow

某处,发送给此的任何邮件地址将进入垃圾,没有我的

,知道它到了。对不起...< http://www.sonic.net/~dakiddfor more info

Because:

&p1 == the address of the variable (parameter, actually) p1

p1 == the variable holding the address of the struct under comparison

*p1 == the struct under comparison

Assuming p1 contains the (randomly chosen) address 5, and assuming p1 is
located in memory at (randomly chosen) location 27, and that the "..."
is replaced by the "const struct mystruct *" as shown above, the
following would be true:

assignment result
....sp1 = &p1 sp now holds 27 (the address of p1)
....sp1 = p1 sp now holds 5 (the contents of p1)
....sp1 = *p1 sp now holds <whatever value is found at location 5>

BUT...
Since p1 is a function parameter, rather than an actual variable, I
believe trying to take its address will result in undefined behavior.
(unless special steps are taken to make things happen differently,
parameters are usually passed on the stack, which makes taking their
address either impossible, or pointless, because for practical purposes,
they don''t really HAVE an address to take.)

--
Don Bruder - da****@sonic.net - If your "From:" address isn''t on my whitelist,
or the subject of the message doesn''t contain the exact text "PopperAndShadow"
somewhere, any message sent to this address will go in the garbage without my
ever knowing it arrived. Sorry... <http://www.sonic.net/~dakiddfor more info




为什么不尝试,看看会发生什么?


了解原因<<扰流板警告>编译器抱怨,

问自己:'& p1'的类型是什么?什么`& p1''

指向什么?要了解FAQ的代码,请问自己:

'p1'指向什么?


-

Eric Sosman
lid

Why don''t you try it, and see what happens?

To understand why <<spoiler alert>the compiler complains,
ask yourself: What is the type of `&p1''? And what does `&p1''
point to? And to understand the FAQ''s code, ask yourself:
What does `p1'' point to?

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


这篇关于来自c faq的关于13.9的问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 09:48