奇怪的代码 - 合法??? [英] Weird bit of code-- legal???

查看:49
本文介绍了奇怪的代码 - 合法???的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前段时间我不得不重新编译一些旧的代码,最初是由一个非常优秀的程序员编写的,但有一个人倾向于使用书中的每一个技巧,

和然后一些。


有一个声明,如下:

结果=(模式== 2?TwoWayFunc:FourWayFunc)(Param1,
Param2,Param3);


...经过一番小小的尝试后我发现了它,但是...这是

合法的C?它使用Microsoft C 2.6左右的编译器进行编译,但是当然没有多少帮助。

解决方案

< blockquote>

Ancient_Hacker写道:


一段时间后我不得不重新编译一些旧代码,最初由一个
$ b写的$ b非常优秀的程序员,但是一个人倾向于使用书中的每一个技巧,

然后一些。


有一个声明,如下所示:


结果=(模式== 2?TwoWayFunc:FourWayFunc)(Param1,

Param2,Param3);


...经过一番小小的讨论之后,我发现了它,但是......这是否在C中是合法的?b $ b合法?它使用Microsoft C 2.6左右的编译器进行编译,但是当然没有多少帮助。



这段代码非常简单,任何问题都不应该给一个体面的C $ / $
程序员。


该行基本上是一个函数调用,将结果放入Result。

根据Mode的值(是2?),它将调用函数

由TwoWayFunc或FourWayFunc指出;通过Params 1到

3.


为什么它不合法,为什么会引起任何头部刮伤?




gbost ... @ excite.com写道:


Ancient_Hacker写道:


前段时间我不得不重新编译一些旧代码,最初是由一个非常好的程序员编写的,但是一个人很容易使用每个技巧在书中,

然后一些。


有一个声明,如下:

结果=(Mode = = 2?TwoWayFunc:FourWayFunc)(Param1,

Param2,Param3);


...经过一番小小的尝试后我发现了它,但是......这是否在C中合法
合法?它使用Microsoft C 2.6左右的编译器进行编译,但是当然没有多少帮助。



这段代码非常简单,任何问题都不应该给一个体面的C $ / $
程序员。


该行基本上是一个函数调用,将结果放入Result。

根据Mode的值(是2?),它将调用函数

由TwoWayFunc或FourWayFunc指出;通过Params 1到

3.


为什么它不合法,为什么会引起任何头部刮伤?



....因为我没有提及,函数名称只是 - 不是

变量持有函数地址。我不知道你可以在条件表达式中使用原始函数名。
然后再说一遍,我b $ b并不知道,我怀疑很多人不知道参数之前的那个东西是一个实际的表达式。


Ancient_Hacker发布:


结果=(模式== 2?TwoWayFunc:FourWayFunc)(Param1 ,

Param2,Param3);


...经过一番小小的尝试后我发现了它,但......这就是这个

合法的C?



不夸耀,但乍一看我理解了代码行。


" TwoWayFunc"和FourWayFunc可能是两个函数指针同一类型的




" Mode == 2"评估,并根据结果,使用参数Param1,Param2,Param3调用

函数中的任何一个。


返回函数调用的值存储在结果中。


代码完全合法。


-


Frederick Gotham


A while back I had to recompile some old code, originally written by a
really good programmer, but one prone to use every trick in the book,
and then some.

There was a statement, something like this:
Result = ( Mode == 2 ? TwoWayFunc : FourWayFunc ) ( Param1,
Param2, Param3 );

... after a little headscratching I figured it out, but ... is this
legal in C? It compiled with an Microsoft C 2.6 or so compiler, but
of course that isnt much help.

解决方案


Ancient_Hacker wrote:

A while back I had to recompile some old code, originally written by a
really good programmer, but one prone to use every trick in the book,
and then some.

There was a statement, something like this:
Result = ( Mode == 2 ? TwoWayFunc : FourWayFunc ) ( Param1,
Param2, Param3 );

... after a little headscratching I figured it out, but ... is this
legal in C? It compiled with an Microsoft C 2.6 or so compiler, but
of course that isnt much help.

This code is really quite simple and shouldn''t give a decent C
programmer any problems whatsoever.

The line is basically a function call that puts the result in Result.
Depending on the value of Mode (is it 2?) it will invoke the function
pointed to by TwoWayFunc or FourWayFunc; passing the Params 1 through
3.

Why shouldn''t it be legal and why would it cause any head scratching?



gbost...@excite.com wrote:

Ancient_Hacker wrote:

A while back I had to recompile some old code, originally written by a
really good programmer, but one prone to use every trick in the book,
and then some.

There was a statement, something like this:
Result = ( Mode == 2 ? TwoWayFunc : FourWayFunc ) ( Param1,
Param2, Param3 );

... after a little headscratching I figured it out, but ... is this
legal in C? It compiled with an Microsoft C 2.6 or so compiler, but
of course that isnt much help.


This code is really quite simple and shouldn''t give a decent C
programmer any problems whatsoever.

The line is basically a function call that puts the result in Result.
Depending on the value of Mode (is it 2?) it will invoke the function
pointed to by TwoWayFunc or FourWayFunc; passing the Params 1 through
3.

Why shouldn''t it be legal and why would it cause any head scratching?


.... because I didnt mention, the function names are just that-- not
variables holding the function addresses. I did not know you could
use raw function names in a conditional expression. Then again, I
didnt know and I suspect a lot of folks don''t know that the thing
before the parameters is an actual "expression".


Ancient_Hacker posted:

Result = ( Mode == 2 ? TwoWayFunc : FourWayFunc ) ( Param1,
Param2, Param3 );

... after a little headscratching I figured it out, but ... is this
legal in C?


Not to boast, but I understood the line of code at first glance.

"TwoWayFunc" and "FourWayFunc" are pressumably both function pointers of the
same type.

"Mode == 2" is evaluated, and depending on the result, either of the
functions is called with the arguments, "Param1, Param2, Param3".

The return value of the function call is stored in "Result".

The code is perfectly legal.

--

Frederick Gotham


这篇关于奇怪的代码 - 合法???的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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