类型转换 [英] Casts

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

问题描述




我还在写关于C的教程书。这是关于

演员的部分。我对你对此的看法感兴趣。有些人

对这个主题有明确的看法(从不使用演员阵容)其他一些人

(像我一样)更自由。在这个主题。我会对任何反馈意见

感兴趣。


jacob

------------- -------------------------------------------------- ----

投射

投射表达式是将对象从一种类型转换为另一种类型的b / b
。例如,一个常见的需求是将双精度

数转换为整数。这是这样指定的:

双d;

....

(int)d

In在这种情况下,演员需要调用运行时代码来进行实际的转换。在其他情况下,根本没有发出代码。对于

实例:

void * p;

....

(char *)p;

在大多数实现中,将一种类型的指针转​​换为另一种指针在

运行时根本不需要代码。


什么时候到使用强制转换


强制转换可以避免不必要的操作。例如,

给出声明:


double double_value;

int integer_value;


如果你写了


integer_value / = double_value;


整数被提升为double以进行除法,结果是

再次转换为整数。然而,通过演员表,可以避免不必要的

促销:


integer_value / =(int)double_value;


您可以在另一个上下文中使用强制转换表达式,以指示复合常量文字的类型

。例如:

typedef struct tagPerson {

char Name [75];

int age;

} Person ;


无效流程(人*);

....

流程(&(人){?Mary史密斯?,38});

这是C99的新功能之一。文字应该用大括号括起来
,它应该与预期的结构相匹配。这只是

?句法糖?对于以下内容:


人__998815544ss = {?Mary Smith?,38};

流程(& __ 998815544ss);

优势在于,现在您可以免除为结构找出

名称的任务,因为编译器会为您执行此操作。内部

然而,该代码恰好代表lcc-win32内发生的事情。


何时不使用演员表


与上述任何其他构造一样,Casts可能被滥用。一般来说,

他们几乎不可能自动遵循类型层次结构。

C是弱类型的,大部分是?弱点?来自演员表达。


--------------------------------- -------------------------------------

Hi

I am still writing my tutorial book about C. Here is the section about
casts. I would be interested in your opinions about this. Some people
have definite views about this subject ("never use casts") some others
(like me) are more "liberal" in this subject. I would be interested
in any feedback.

jacob
-------------------------------------------------------------------
Casts
A cast expression is the transformation of an object from one type to
another. For instance, a common need is to transform double precision
numbers into integers. This is specified like this:
double d;
....
(int)d
In this case, the cast needs to invoke run time code to make the actual
transformation. In other cases there is no code emitted at all. For
instance in:
void *p;
....
(char *)p;
Transforming one type of pointer into another needs no code at all at
run-time in most implementations.

When to use casts

A cast can be useful to avoid unnecessary operations. For instance,
given the declarations:

double double_value;
int integer_value;

If you write

integer_value /= double_value;

The integer is promoted to double to do the division, then the result is
converted to integer again. With a cast however, the unnecessary
promotions can be avoided:

integer_value /= (int)double_value;

You can use a cast expression in another context, to indicate the type
of a composite constant literal. For instance:
typedef struct tagPerson {
char Name[75];
int age;
} Person;

void process(Person *);
....
process(&(Person){?Mary Smith? , 38});
This is one of the new features of C99. The literal should be enclosed
in braces, and it should match the expected structure. This is just
?syntactic sugar? for the following:

Person __998815544ss = { ?Mary Smith?, 38};
process(&__998815544ss);

The advantage is that now you are spared that task of figuring out a
name for the structure since the compiler does that for you. Internally
however, that code represents exactly what happens inside lcc-win32.

When not to use casts

Casts, as any other of the constructs above, can be misused. In general,
they make almost impossible to follow the type hierarchy automatically.
C is weakly typed, and most of the ?weakness? comes from casts expressions.

----------------------------------------------------------------------

推荐答案

jacob navia写道:
jacob navia wrote:

[...]

演员阵容可以避免不必要的操作。例如,

给出声明:


double double_value;

int integer_value;


如果你写了


integer_value / = double_value;


整数被提升为double以进行除法,结果是

再次转换为整数。然而,通过演员表,可以避免不必要的

促销:


integer_value / =(int)double_value;
[...]
A cast can be useful to avoid unnecessary operations. For instance,
given the declarations:

double double_value;
int integer_value;

If you write

integer_value /= double_value;

The integer is promoted to double to do the division, then the result is
converted to integer again. With a cast however, the unnecessary
promotions can be avoided:

integer_value /= (int)double_value;



值得一提的是,修改后的表达式

可能会产生与原始值不同的值。例如,

考虑integer_value = 10和double_value = 3.5,其中

原始代码计算2,修改后的代码计算3.

(更好的是:考虑double_value = 0.1 ...)

It would be worth mentioning that the modified expression
may produce a different value than the original. For example,
consider integer_value=10 and double_value=3.5, where the
original code calculates 2 and the revised code calculates 3.
(Even better: consider double_value=0.1 ...)


您可以在另一个上下文中使用强制转换表达式来表示类型

of复合常量字面量。例如:

typedef struct tagPerson {

char Name [75];

int age;

} Person ;


无效流程(人*);

...

流程(&(人){?Mary Smith ?,38});
You can use a cast expression in another context, to indicate the type
of a composite constant literal. For instance:
typedef struct tagPerson {
char Name[75];
int age;
} Person;

void process(Person *);
...
process(&(Person){?Mary Smith? , 38});



这不是演员表,所以它不属于关于演员表的教程

。 (除非,或许可以解释说,尽管有一种模糊的表面相似之处,但它并没有演绎出来。)


-

Eric Sosman
es*****@acm-dot-org.inva lid

This is not a cast, so it doesn''t belong in a tutorial
about casts. (Except, perhaps, to explain that it is not
a cast despite a faint and superficial syntactic resemblance.)

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




" jacob navia" < ja *** @ jacob.remcomp.frwrote in message

news:46 ********************** @ news。 orange.fr ...

"jacob navia" <ja***@jacob.remcomp.frwrote in message
news:46**********************@news.orange.fr...




我还在写关于C的教程书。这是关于

施放。我会对你对这个

无效流程(人*)的意见感兴趣;

...

流程(&(人) {?Mary Smith?,38});

这是C99的新功能之一。然而,在内部,代码

代表lcc-win32内发生的事情。
Hi

I am still writing my tutorial book about C. Here is the section about
casts. I would be interested in your opinions about this
void process(Person *);
...
process(&(Person){?Mary Smith? , 38});
This is one of the new features of C99. Internally however, that code
represents exactly what happens inside lcc-win32.



我认为你应该写一个C89教程,一个C99教程,或者一个

lcc-win32教程。我没有看到讨论C99结构的意义和

然后lcc-win如何实现它们,如果读者坐在C89

编译器上。


它也改变了这种情况。如果演员阵容是分配

文字结构的语法的一部分,那么很明显它会修改你想要的任何东西

说C casts。


-

免费游戏和编程好东西。
http://www.personal.leeds.ac.uk/~bgy1mm


jacob navia说:


< snip>
jacob navia said:

<snip>

强制转换

强制转换表达式是对象的转换从一种类型到另一种类型的

Casts
A cast expression is the transformation of an object from one type to
another.



不,不是。这是*值*从一种类型转换为

另一种类型。


< snip>

No, it isn''t. It is the transformation of a *value* from one type to
another.

<snip>


例如:

void * p;

...

(char *)p;
For instance in:
void *p;
...
(char *)p;



我不能想到将一个void *转换为char *的一个好理由。

I can''t think of a single good reason for casting a void * to a char *.


何时使用演员表


演员表可以避免不必要的操作。例如,

给出声明:


double double_value;

int integer_value;


如果你写的话


integer_value / = double_value;


将整数提升为double以进行除法,然后结果

再次转换为整数。然而,通过演员表,可以避免不必要的

促销:


integer_value / =(int)double_value;
When to use casts

A cast can be useful to avoid unnecessary operations. For instance,
given the declarations:

double double_value;
int integer_value;

If you write

integer_value /= double_value;

The integer is promoted to double to do the division, then the result
is converted to integer again. With a cast however, the unnecessary
promotions can be avoided:

integer_value /= (int)double_value;



....以转换为int为代价,因此节省的金额不会高于

你领先读者要相信。


< snipped:Eric已经讨论过的一点,就是&str;


你完全没有提到(无可否认的是很少)地方

其中演员实际上是一个好主意。

....at the cost of a conversion to int, so the saving isn''t as great as
you are leading the reader to believe.

<snipped: a point that Eric has already discussed elsethread>

You''ve completely failed to mention the (admittedly very few) places
where casts are actually a good idea.


什么时候不使用演员表


与上述任何其他构造一样,Casts可能被滥用。在

一般情况下,他们几乎不可能自动遵循类型层次结构

。 C是弱类型的,大多数的弱点?来自强制转换表达式的
来自。
When not to use casts

Casts, as any other of the constructs above, can be misused. In
general, they make almost impossible to follow the type hierarchy
automatically. C is weakly typed, and most of the ?weakness? comes
from casts expressions.



您可能希望在该段落上获得校对员。


-

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

电子邮件:-www。 + rjh @

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

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

You might want to get a proofreader onto that paragraph.

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


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

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