优势在于,现在您可以免除为结构找出 名称的任务,因为编译器会为您执行此操作。内部 然而,该代码恰好代表lcc-win32内发生的事情。 何时不使用演员表 与上述任何其他构造一样,Casts可能被滥用。一般来说, 他们几乎不可能自动遵循类型层次结构。 C是弱类型的,大部分是?弱点?来自演员表达。 --------------------------------- -------------------------------------HiI am still writing my tutorial book about C. Here is the section aboutcasts. I would be interested in your opinions about this. Some peoplehave definite views about this subject ("never use casts") some others(like me) are more "liberal" in this subject. I would be interestedin any feedback.jacob-------------------------------------------------------------------CastsA cast expression is the transformation of an object from one type toanother. For instance, a common need is to transform double precisionnumbers into integers. This is specified like this:double d;....(int)dIn this case, the cast needs to invoke run time code to make the actualtransformation. In other cases there is no code emitted at all. Forinstance in:void *p;....(char *)p;Transforming one type of pointer into another needs no code at all atrun-time in most implementations.When to use castsA cast can be useful to avoid unnecessary operations. For instance,given the declarations:double double_value;int integer_value;If you writeinteger_value /= double_value;The integer is promoted to double to do the division, then the result isconverted to integer again. With a cast however, the unnecessarypromotions can be avoided:integer_value /= (int)double_value;You can use a cast expression in another context, to indicate the typeof 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 enclosedin 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 aname for the structure since the compiler does that for you. Internallyhowever, that code represents exactly what happens inside lcc-win32.When not to use castsCasts, 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 writeinteger_value /= double_value;The integer is promoted to double to do the division, then the result isconverted to integer again. With a cast however, the unnecessarypromotions 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 expressionmay produce a different value than the original. For example,consider integer_value=10 and double_value=3.5, where theoriginal 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 typeof 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 lidThis is not a cast, so it doesn''t belong in a tutorialabout casts. (Except, perhaps, to explain that it is nota 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 messagenews:46**********************@news.orange.fr... 嗨 我还在写关于C的教程书。这是关于 施放。我会对你对这个 无效流程(人*)的意见感兴趣; ... 流程(&(人) {?Mary Smith?,38}); 这是C99的新功能之一。然而,在内部,代码 代表lcc-win32内发生的事情。HiI am still writing my tutorial book about C. Here is the section aboutcasts. I would be interested in your opinions about thisvoid process(Person *);...process(&(Person){?Mary Smith? , 38});This is one of the new features of C99. Internally however, that coderepresents 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> 强制转换 强制转换表达式是对象的转换从一种类型到另一种类型的。CastsA cast expression is the transformation of an object from one type toanother. 不,不是。这是*值*从一种类型转换为 另一种类型。 < snip>No, it isn''t. It is the transformation of a *value* from one type toanother.<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 castsA cast can be useful to avoid unnecessary operations. For instance,given the declarations:double double_value;int integer_value;If you writeinteger_value /= double_value;The integer is promoted to double to do the division, then the resultis converted to integer again. With a cast however, the unnecessarypromotions 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 asyou 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) placeswhere casts are actually a good idea. 什么时候不使用演员表 与上述任何其他构造一样,Casts可能被滥用。在 一般情况下,他们几乎不可能自动遵循类型层次结构 。 C是弱类型的,大多数的弱点?来自强制转换表达式的来自。When not to use castsCasts, as any other of the constructs above, can be misused. Ingeneral, they make almost impossible to follow the type hierarchyautomatically. C is weakly typed, and most of the ?weakness? comesfrom 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 这篇关于类型转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-24 02:13