理查德希思菲尔德的铸造操作 [英] Richard heathfields casting operation

查看:85
本文介绍了理查德希思菲尔德的铸造操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在阅读彼得范德林登的专家C编程书,

在本书中有一个名为 ;如何以及为什么施放


作者然后说如下


(浮动)3 - 它是类型转换和实际比特改变。

如果你说(浮动)3.0它是一种类型消歧,编译器

可以在第一时间种植正确的位。有些人说

演员阵容之所以如此命名,是因为他们帮助了一些破碎的东西。


究竟是什么类型的消歧...... ????

i没有看过任何类似的演员(浮动)3.0;在使用中(可能是由于我的b $ b bb经验有限)。它将如何帮助编译器植入

正确的位????


然后作者说,作为一个不切实际的例子,你可以创建一个

指向,例如printf(),


extern int printf(const char *,...);

voif * f =(void *)printf;


然后你可以通过一个正确的指针调用printf,用这个

方式:


(*(int(*)(const char *,...))f)(咬我的短裤。还有我的字符和整数

\ n );


now void * f =(void *)printf;为什么这个演员是必需的.... ????

^^^^^^

为什么作者说上面的例子是不切实际的...... ???

为什么它是因为库函数不是要通过

无效指针调用..... ?????


解释上述问题我去了Richard Heathfields网站:
http://www.cpax.org.uk/prg/writings/casting.php


在那里我没有发现任何关于类型消歧的提及..

但我找到了更复杂的例子,比如这个


(int)((int(*)(const char *,...))

printf((const char *)"%3.0f%6.1f \ n",

(float)fahr,(float)celsius));


这个陈述到底是做什么的...... ???


就是这样简单的printf ....

printf("%3.0f%6.1f \ n",fahr,celsius);


为什么所有这些冗余演员都成功h as


1)(const char *)"%3.0f%6.1f \ n"

2)(int)((int( *)(const char *,...))

解决方案

所以********** @ gmail.com 说:





我正在阅读彼得范德林登的专家C编程书,

在本书中有一个名为如何以及为什么到然后,作者接着说:


(浮动)3 - 它是类型转换,实际位变化。

如果你说(浮动)3.0这是一种类型消歧,编译器

可以在第一时间种植正确的位。有些人说

演员之所以如此命名,是因为他们帮助一些破碎的东西跛行。



在我的版本中,它表示(双倍)3和(双倍)3.0 - 他对

转换是对的,但错误关于消歧,因为3.0的类型是

double,所以将它转换为double并不能消除任何歧义,因为

没有歧义消除歧义。


究竟是什么类型的消歧...... ????



缺席。


我没有看过像(float)3.0这样的演员表;在使用中(可能是由于我的b $ b bb经验有限)。它将如何帮助编译器植入

正确位?



如果你需要一个价值3.0f的浮点数,就说3.0f - 没有必要施展



>

然后作者说,作为一个不切实际的例子,你可以创建一个

指针,例如printf(),


extern int printf(const char *,...);

voif * f =(void *)printf;



虽然这是一个常见的扩展,但标准并不保证你可以将一个函数指针存储在void *中而不会丢失信息。

最好这样做(在这种情况下不需要强制转换):


int(* f)(const char *,...)= printf;


>

然后你可以通过正确投射的指针调用printf,这个

方式:


(*(int(*)(const char *,...))f)(咬我的短裤。还有我的字符和整数

\ n");



不保证书面形式。但是根据我建议的改变,你不需要

施放。你只做(* f)(咬...... \ n);甚至只需f(Bite ... \ n);

由您自行决定。


>

now void * f =(void *)printf;为什么需要这个演员.... ????

^^^^^^



代码被破坏了。不要用它。


为什么作者说上面的例子是不切实际的...... ???



也许是因为代码坏了。


为什么它是因为库函数不是通过

无效指针调用..... ?????



因为标准并不保证它是可能的。


的解释以上问题我去了理查德希思菲尔德网站:
http ://www.cpax.org.uk/prg/writings/casting.php


在那里我没有发现有关类型消歧的提及..



对。没有必要。铸造不是消除歧义。


但我找到了更复杂的例子,比如这个


(int)((int( *)(const char *,...))

printf((const char *)"%3.0f%6.1f \ n",

(浮点数) )fahr,(float)摄氏度));


这个陈述到底是做什么的?



这是一种图片。有些人声称演员阵容澄清码。这张

的照片让他们听从了他们的话。您认为哪个更清楚 -

以上,或printf(%3.0f%6.1f \ n,fahr,摄氏度))?我知道哪一个我会选择。$ />

< snip>


-

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

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

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

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


Richard Heathfield写道:

所以********** @ gmail.com 说:


>你好,

我正在阅读彼得范德林登的专家C编程书,
在本书中有一个名为如何以及为什么要施放的部分
<然后作者说如下

(浮动)3 - 这是一种类型转换,实际位改变。
如果你说(浮动)3.0它是一种类型消歧,并且编译器可以在第一时间植入正确的位。有些人说
强制转换是如此命名的,因为它们有助于破坏的东西跛行。



在我的版本中,它说(双)3和(双)3.0 - 他对

的转换是对的,但错了关于消歧,因为3.0的类型是

double,所以将它转换为double并不能消除任何歧义,因为

没有歧义消除歧义。


>这种类型的消歧究竟是什么...... ????



缺席。


>我没见过像(浮动)3.0这样的演员表;在使用中(可能是由于我的经验有限)。它将如何帮助编译器种植正确的位?



如果你需要一个价值3.0f的浮点数,就说3.0f - 没有必要施展



>然后作者说,作为一个不切实际的例子,你可以创建一个
指针,例如printf(),
extern int printf(const char *,...);
voif * f =(void *)printf;



虽然这是一个常见的扩展,但标准并不能保证你能够在不丢失信息的情况下将函数指针存储在void *中。

最好这样做(在这种情况下不需要强制转换):


int(* f)(const char *,...)= printf;


>然后您可以通过正确投射的指针调用printf,这种方式:

(*( int(*)(const char *,...))f)(咬我的短裤。还有我的字符和注册
\ n);



不保证书面形式。但是根据我建议的改变,你不需要

施放。你只做(* f)(咬...... \ n);甚至只需f(Bite ...... \ n);

由您自行决定。


> now void * f =(void *)printf;为什么需要这个演员...... ????
^^^^^^



代码坏了。不要用它。


>为什么作者说上面的例子是不切实际的...... ???



也许是因为代码坏了。


>为什么它是因为库函数不是通过
无效指针调用..... ?????



因为标准并不保证它是可能的。


> for上述问题的解释我去了理查德希思菲尔德网站:
http://www.cpax.org.uk/prg/writings/casting.php

在那里我没有发现有关类型消歧的提及..



对。没有必要。铸造不是消除歧义。


>但我找到了更复杂的例子,例如这个(int)((int(*) (const char *,...))
printf((const char *)"%3.0f%6.1f \ n",
(float)fahr,(float)celsius));

这句话到底是做什么的?



这是一种图片。有些人声称演员阵容澄清码。这张

的照片让他们听从了他们的话。您认为哪个更清楚 -

以上,或printf(%3.0f%6.1f \ n,fahr,摄氏度))?我知道我会选择哪一个b $ b。



它不会调用printf,将结果转换为''int(*)(const char *,...)'',

然后再把它再次投入''int''?因此不应该是:


(int)(

((int(*)(const char *,...))printf)

((const char *)"%3.0f%6.1f \ n",(float)fahr,(float)celsius)

);


如果没有,这并不意味着(无意义)演员如下:


int * x =(int *)malloc( sizeof * x); / *不要这样做。 * /


实际上是将malloc而不是malloc的返回值转换为''int *''?


-

Philip Potter pgp< atdoc.ic.ac.uk


11月7日上午7:05,Richard Heathfield< r .. 。@ see.sig.invalidwrote:


sophia.ag ... @ gmail.com说:




我正在阅读peter van der linden的书籍Expert C编程,

in this书上有一个名为如何以及为什么要施放的部分。


作者接着说如下


(float)3 - 它是类型转换和实际位改变。

如果你说(浮点)3.0它是一种类型消歧,编译器

可以种植正确的位首先,有些人说

演员阵容是如此命名的,因为他们帮助了一些破碎的东西。



在我的版本中,它说(双)3和(双)3.0 - 他对

的转换是对的,但错了关于消歧,因为3.0的类型是

double,所以将它转换为double并不能消除任何歧义,因为

没有歧义消除歧义。


究竟是什么类型的消歧...... ????



缺席。


我没见过像(浮动)3.0这样的演员表;在使用中(可能是由于我的b $ b bb经验有限)。它将如何帮助编译器植入

正确位?



如果你需要一个价值3.0f的浮点数,就说3.0f - 没有必要施展



然后作者说,作为一个不切实际的例子,你可以创建一个

指针,例如printf(),


extern int printf(const char *,...);

voif * f =(void *)printf;



虽然这是一个常见的扩展,但标准并不能保证你能够在不丢失信息的情况下将函数指针存储在void *中。

最好这样做(在这种情况下不需要强制转换):


int(* f)(const char *,...)= printf;


然后你可以通过一个正确的指针调用printf,这个方式是:

< blockquote class =post_quotes>
(*(int(*)(const char *,...))f)(咬我的短裤。还有我的字符和整数

\\\
");



不保证书面形式。但是根据我建议的改变,你不需要

施放。你只做(* f)(咬...... \ n);甚至只需f(Bite ... \ n);

由您自行决定。


now void * f = (void *)printf;为什么需要这个演员.... ????

^^^^^^



代码坏了。不要用它。


为什么作者说上面的例子是不切实际的...... ???



也许是因为代码坏了。


为什么它是因为库函数不是通过

无效指针调用..... ?????



因为标准并不保证它是可能的。


的解释以上问题我去了理查德希思菲尔德网站:
http ://www.cpax.org.uk/prg/writings/casting.php


在那里我没有发现有关类型消歧的提及..



对。没有必要。铸造不是消除歧义。


但我找到了更复杂的例子,比如这个


(int)((int(*)(const char *,...))

printf((const char *)"%3.0f%6.1f \ n",

(浮动)fahr,(浮动)摄氏度));


这句话到底是做什么的?



这是一种图片。有些人声称演员阵容澄清码。这张

的照片让他们听从了他们的话。您认为哪个更清楚 -

以上,或printf(%3.0f%6.1f \ n,fahr,摄氏度))?我知道哪一个我会选择。$ />

< snip>


-

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

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

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

Usenet是一个奇怪的放置" - dmr 1999 $ 7 $ b



i编译并执行你的程序: -


#include< stdio.h>

main()

{

float fahr,摄氏度;

int lower,upper,step;


lower =(int)0; / *温度表的下限* /

upper =(int)300; / *上限* /

step =(int)20; / *步长* /


fahr =(浮动)降低;

while((float)fahr< =(int)upper){

摄氏度=(((浮动)

((浮动)5.0 /(浮动)9.0))*

(浮动)((浮动)

(((float)fahr-(float)32.0))));


(int)((int(*)(const char * ,. ..))

printf((const char *)"%3.0f%6.1f \ n",

(float)fahr,(float)摄氏度) );


fahr =(浮动)((浮动)fahr +(浮动)步骤);

}


gcc -o cast casting.c

../casting

i得到以下结果: -


0 -17.8

20 -6.7

40 4.4

60 15.6

80 26.7

100 37.8

120 48.9

140 60.0

160 71.1

180 82.2

200 93.3

220 104.4

240 115.6

260 126.7

280 137.8

300 148.9

我手动完成了一个val的计算ue使用计算器

为你的220 104.4值计算器o / p是

219.92 104.4

(根据摄氏值计算的fahr值计算) 104.4)

所以你的程序不准确值为.08


Hi ,

I was going through peter van der linden''s book Expert C programming,
in this book there is a section named "How and why to cast"

the author then says as follows

(float) 3 - it''s a type conversion and the actual bits change.
if you say (float) 3.0 it is a type disambiguation,and the compiler
can plant the correct bits in the first place.some people say that
casts are so named because they help something broken to limp along.

what exactly is this type disambiguation...????
i have n''t seen any casts like (float) 3.0; in use (may be due to my
limited experience). how will it help the compiler to plant the
correct bits????

Then the author says that as an impratical example ,you can create a
pointer to ,for example printf(), with

extern int printf(const char*,...);
voif *f = (void *) printf;

you can then call printf through a properly cast pointer, in this
manner:

(*(int(*)(const char*,...))f)("Bite my shorts. Also my chars and ints
\n");

now void *f = (void *) printf; why this cast is required....????
^^^^^^
why the author says that above example is impractical...???
why is it because library functions are not meant to be called via
void pointer.....?????

for explanation of above issues i went to Richard heathfields site:
http://www.cpax.org.uk/prg/writings/casting.php

there i found nothing mentioned about type disambiguation..
but i found more complex example such as this

(int)((int (*)(const char *, ...))
printf((const char *)"%3.0f %6.1f\n",
(float)fahr, (float)celsius));

what exactly does this statement does...???

is it just simple printf such as this....
printf("%3.0f %6.1f\n",fahr, celsius);

why all these redundant casts such as

1)(const char *)"%3.0f %6.1f\n"
2)(int)((int (*)(const char *, ...))

解决方案

so**********@gmail.com said:

Hi ,

I was going through peter van der linden''s book Expert C programming,
in this book there is a section named "How and why to cast"

the author then says as follows

(float) 3 - it''s a type conversion and the actual bits change.
if you say (float) 3.0 it is a type disambiguation,and the compiler
can plant the correct bits in the first place.some people say that
casts are so named because they help something broken to limp along.

In my edition, it says (double) 3 and (double) 3.0 - he''s right about the
conversion but wrong about the disambiguation, since 3.0 is of type
double, so casting it to double doesn''t disambiguate anything, as there is
no ambiguity to disambiguate.

what exactly is this type disambiguation...????

Absent.

i have n''t seen any casts like (float) 3.0; in use (may be due to my
limited experience). how will it help the compiler to plant the
correct bits????

If you need a float with the value 3.0f, just say 3.0f - there is no need
to cast.

>
Then the author says that as an impratical example ,you can create a
pointer to ,for example printf(), with

extern int printf(const char*,...);
voif *f = (void *) printf;

Whilst this is a common extension, the Standard does not guarantee that you
can store a function pointer in a void * without losing information.
Better to do this (in which case no cast is required):

int (*f)(const char *, ...) = printf;

>
you can then call printf through a properly cast pointer, in this
manner:

(*(int(*)(const char*,...))f)("Bite my shorts. Also my chars and ints
\n");

Not guaranteed as written. But with my suggested change, you don''t need to
cast. You just do either (*f)("Bite...\n"); or even simply f("Bite...\n");
at your discretion.

>
now void *f = (void *) printf; why this cast is required....????
^^^^^^

The code is broken. Don''t use it.

why the author says that above example is impractical...???

Perhaps because the code is broken.

why is it because library functions are not meant to be called via
void pointer.....?????

Because the Standard doesn''t guarantee that it''s possible.

for explanation of above issues i went to Richard heathfields site:
http://www.cpax.org.uk/prg/writings/casting.php

there i found nothing mentioned about type disambiguation..

Right. There''s no need for it. Casting is not about disambiguation.

but i found more complex example such as this

(int)((int (*)(const char *, ...))
printf((const char *)"%3.0f %6.1f\n",
(float)fahr, (float)celsius));

what exactly does this statement does...???

It''s a kind of picture. Some people claim that casts "clarify" code. This
picture takes them at their word. Which do you think is clearer - the
above, or printf("%3.0f %6.1f\n", fahr, celsius))? I know which one I''d
choose.

<snip>

--
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


Richard Heathfield wrote:

so**********@gmail.com said:

>Hi ,

I was going through peter van der linden''s book Expert C programming,
in this book there is a section named "How and why to cast"

the author then says as follows

(float) 3 - it''s a type conversion and the actual bits change.
if you say (float) 3.0 it is a type disambiguation,and the compiler
can plant the correct bits in the first place.some people say that
casts are so named because they help something broken to limp along.


In my edition, it says (double) 3 and (double) 3.0 - he''s right about the
conversion but wrong about the disambiguation, since 3.0 is of type
double, so casting it to double doesn''t disambiguate anything, as there is
no ambiguity to disambiguate.

>what exactly is this type disambiguation...????


Absent.

>i have n''t seen any casts like (float) 3.0; in use (may be due to my
limited experience). how will it help the compiler to plant the
correct bits????


If you need a float with the value 3.0f, just say 3.0f - there is no need
to cast.

>Then the author says that as an impratical example ,you can create a
pointer to ,for example printf(), with

extern int printf(const char*,...);
voif *f = (void *) printf;


Whilst this is a common extension, the Standard does not guarantee that you
can store a function pointer in a void * without losing information.
Better to do this (in which case no cast is required):

int (*f)(const char *, ...) = printf;

>you can then call printf through a properly cast pointer, in this
manner:

(*(int(*)(const char*,...))f)("Bite my shorts. Also my chars and ints
\n");


Not guaranteed as written. But with my suggested change, you don''t need to
cast. You just do either (*f)("Bite...\n"); or even simply f("Bite...\n");
at your discretion.

>now void *f = (void *) printf; why this cast is required....????
^^^^^^


The code is broken. Don''t use it.

>why the author says that above example is impractical...???


Perhaps because the code is broken.

>why is it because library functions are not meant to be called via
void pointer.....?????


Because the Standard doesn''t guarantee that it''s possible.

>for explanation of above issues i went to Richard heathfields site:
http://www.cpax.org.uk/prg/writings/casting.php

there i found nothing mentioned about type disambiguation..


Right. There''s no need for it. Casting is not about disambiguation.

>but i found more complex example such as this

(int)((int (*)(const char *, ...))
printf((const char *)"%3.0f %6.1f\n",
(float)fahr, (float)celsius));

what exactly does this statement does...???


It''s a kind of picture. Some people claim that casts "clarify" code. This
picture takes them at their word. Which do you think is clearer - the
above, or printf("%3.0f %6.1f\n", fahr, celsius))? I know which one I''d
choose.

Doesn''t it call printf, cast the result to ''int (*)(const char *,...)'',
then cast it again to ''int''? Shouldn''t it therefore be:

(int)(
((int (*)(const char *, ...))printf)
((const char *)"%3.0f %6.1f\n",(float)fahr, (float)celsius)
);

If not, doesn''t that mean that the (pointless) cast in the following:

int *x = (int *)malloc(sizeof *x); /* Don''t do this. */

is actually casting malloc, not malloc''s return value, to ''int *''?

--
Philip Potter pgp <atdoc.ic.ac.uk


On Nov 7, 7:05 am, Richard Heathfield <r...@see.sig.invalidwrote:

sophia.ag...@gmail.com said:

Hi ,

I was going through peter van der linden''s book Expert C programming,
in this book there is a section named "How and why to cast"

the author then says as follows

(float) 3 - it''s a type conversion and the actual bits change.
if you say (float) 3.0 it is a type disambiguation,and the compiler
can plant the correct bits in the first place.some people say that
casts are so named because they help something broken to limp along.


In my edition, it says (double) 3 and (double) 3.0 - he''s right about the
conversion but wrong about the disambiguation, since 3.0 is of type
double, so casting it to double doesn''t disambiguate anything, as there is
no ambiguity to disambiguate.

what exactly is this type disambiguation...????


Absent.

i have n''t seen any casts like (float) 3.0; in use (may be due to my
limited experience). how will it help the compiler to plant the
correct bits????


If you need a float with the value 3.0f, just say 3.0f - there is no need
to cast.

Then the author says that as an impratical example ,you can create a
pointer to ,for example printf(), with

extern int printf(const char*,...);
voif *f = (void *) printf;


Whilst this is a common extension, the Standard does not guarantee that you
can store a function pointer in a void * without losing information.
Better to do this (in which case no cast is required):

int (*f)(const char *, ...) = printf;

you can then call printf through a properly cast pointer, in this
manner:

(*(int(*)(const char*,...))f)("Bite my shorts. Also my chars and ints
\n");


Not guaranteed as written. But with my suggested change, you don''t need to
cast. You just do either (*f)("Bite...\n"); or even simply f("Bite...\n");
at your discretion.

now void *f = (void *) printf; why this cast is required....????
^^^^^^


The code is broken. Don''t use it.

why the author says that above example is impractical...???


Perhaps because the code is broken.

why is it because library functions are not meant to be called via
void pointer.....?????


Because the Standard doesn''t guarantee that it''s possible.

for explanation of above issues i went to Richard heathfields site:
http://www.cpax.org.uk/prg/writings/casting.php

there i found nothing mentioned about type disambiguation..


Right. There''s no need for it. Casting is not about disambiguation.

but i found more complex example such as this

(int)((int (*)(const char *, ...))
printf((const char *)"%3.0f %6.1f\n",
(float)fahr, (float)celsius));

what exactly does this statement does...???


It''s a kind of picture. Some people claim that casts "clarify" code. This
picture takes them at their word. Which do you think is clearer - the
above, or printf("%3.0f %6.1f\n", fahr, celsius))? I know which one I''d
choose.

<snip>

--
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

i compiled and executed your program:-

#include <stdio.h>
main()
{
float fahr, celsius;
int lower, upper, step;

lower = (int)0; /* lower limit of temperature table */
upper = (int)300; /* upper limit */
step = (int)20; /* step size */

fahr = (float)lower;
while((float)fahr <= (int)upper) {
celsius = (((float)
((float)5.0/(float)9.0)) *
(float)((float)
(((float)fahr-(float)32.0))));

(int)((int (*)(const char *, ...))
printf((const char *)"%3.0f %6.1f\n",
(float)fahr, (float)celsius));

fahr = (float)((float)fahr + (float)step);
}

gcc -o casting casting.c
../casting

i got the following result:-

0 -17.8
20 -6.7
40 4.4
60 15.6
80 26.7
100 37.8
120 48.9
140 60.0
160 71.1
180 82.2
200 93.3
220 104.4
240 115.6
260 126.7
280 137.8
300 148.9

I manually did the calculation for one value by using calculator
for your 220 104.4 values calculator o/p is
219.92 104.4
(calculation done for fahr value based on celsius value 104.4)
so your program is not accurate by a value of .08


这篇关于理查德希思菲尔德的铸造操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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