typedef用于转换函数指针 [英] typedef Used to Cast Function Pointers

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

问题描述

是否可以使用typedef来转换函数指针?我想我在WINGs src中看到了这个
; grep for''(hashFunc)''。到目前为止,尝试
使用typedef来转换函数指针,以便将返回值

float更改为float似乎不起作用;我相信它就像是

这个:


typedef int(* foo)(int);


function_ptr_bar =(foo)function_that_returns_float;

Is is possible to use typedefs to cast function pointers? I think I
saw this in the WINGs src; grep for ''(hashFunc)''. So far, trying to
use a typedef to cast function pointers so that a return value of
float is changed to float seems not to work; I belive it was like
this:

typedef int (*foo) (int);

function_ptr_bar = (foo)function_that_returns_float;

推荐答案

Barbrawl McBribe写道:
Barbrawl McBribe wrote:
是否可以使用typedef来cast函数指针?


只有一个演员表达可以用来演员。

我想我在WINGs src中看到了这个; grep for''(hashFunc)''。到目前为止,尝试使用typedef来转换函数指针,以便将返回值
float更改为float似乎不起作用;我相信它就像
这样:

typedef int(* foo)(int);


这将foo声明为类型为指向函数的指针的别名,类型为int,返回int" ;.

function_ptr_bar =(foo)function_that_returns_float;
Is is possible to use typedefs to cast function pointers?
Only a cast expression can be used to cast.
I think I
saw this in the WINGs src; grep for ''(hashFunc)''. So far, trying to
use a typedef to cast function pointers so that a return value of
float is changed to float seems not to work; I belive it was like
this:

typedef int (*foo) (int);
This declares foo as an alias for type "pointer to function taking 1
argument of type int and returning int".

function_ptr_bar = (foo)function_that_returns_float;




这使用强制转换表达式从一种类型转换为另一种类型。我不认为
相信你可以通过生成的指针安全地调用该函数

,除非它首先被转换回正确的类型。

-Kevin

-

我的电子邮件地址有效,但会定期更改。

要联系我,请使用地址来自最近的帖子。



This uses a cast expression to convert from one type to another. I don''t
believe you can safely call the function through the resulting pointer
unless it is first cast back to the correct type.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.


fi **** ***@thelinuxlink.net (Barbrawl McBribe)写道:
fi*******@thelinuxlink.net (Barbrawl McBribe) writes:
是否可以使用typedef来转换函数指针?我想我在WINGs src看到了这个; grep for''(hashFunc)''。到目前为止,尝试使用typedef来转换函数指针,以便将返回值
float更改为float似乎不起作用;我相信它就像
这样:

typedef int(* foo)(int);

function_ptr_bar =(foo)function_that_returns_float;
Is is possible to use typedefs to cast function pointers? I think I
saw this in the WINGs src; grep for ''(hashFunc)''. So far, trying to
use a typedef to cast function pointers so that a return value of
float is changed to float seems not to work; I belive it was like
this:

typedef int (*foo) (int);

function_ptr_bar = (foo)function_that_returns_float;




我认为你的意思是所以浮动的返回值改为

int。


为什么你想要这样做吗?你可以使它工作,但是它是非常不安全和不便携的。基本上,你正试图向编译器撒谎。充其量,你会调用function_that_returns_float()

并将其结果视为int(不是数字转换,而是对位进行重新解释)。在最坏的情况下,调用约定将会不同,并且会发生任意不好的事情,从获得无效的垃圾到崩溃你的程序。


-

Keith Thompson(The_Other_Keith) ks***@mib.org < http ://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://www.sdsc.edu/~kst>

Schroedinger做莎士比亚:要*和*不要

(注意新的e邮件地址)



I presume you mean "so that a return value of float is changed to
int".

Why would you want to do this? You can probably make it work, but
it''s extremely unsafe and non-portable. Basically, you''re trying to
lie to the compiler. At best, you''ll call function_that_returns_float()
and treat its result as an int (not a numeric conversion, but a
reinterpretation of the bits). At worst, the calling conventions will
differ, and arbitrarily bad things will happen, from getting
meaningless garbage to crashing your program.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
(Note new e-mail address)





Keith Thompson写道:


Keith Thompson wrote:
fi ******* @ thelinuxlink.net (Barbrawl McBribe)写道:
fi*******@thelinuxlink.net (Barbrawl McBribe) writes:
是可以使用typedef来转换函数指针吗?我想我在WINGs src看到了这个; grep for''(hashFunc)''。到目前为止,尝试使用typedef来转换函数指针,以便将返回值
float更改为float似乎不起作用;我相信它就像
这样:

typedef int(* foo)(int);

function_ptr_bar =(foo)function_that_returns_float;
Is is possible to use typedefs to cast function pointers? I think I
saw this in the WINGs src; grep for ''(hashFunc)''. So far, trying to
use a typedef to cast function pointers so that a return value of
float is changed to float seems not to work; I belive it was like
this:

typedef int (*foo) (int);

function_ptr_bar = (foo)function_that_returns_float;



我认为你的意思是所以浮动的返回值改为
int。

你为什么要这样做?你可以使它工作,但它是非常不安全和不便携的。基本上,你试图欺骗编译器。充其量,你会调用function_that_returns_float()
并将其结果视为int(不是数字转换,而是对位的重新解释)。在最坏的情况下,调用约定会有所不同,并且会发生任意不好的事情,从获得无意义的垃圾到崩溃你的程序。


I presume you mean "so that a return value of float is changed to
int".

Why would you want to do this? You can probably make it work, but
it''s extremely unsafe and non-portable. Basically, you''re trying to
lie to the compiler. At best, you''ll call function_that_returns_float()
and treat its result as an int (not a numeric conversion, but a
reinterpretation of the bits). At worst, the calling conventions will
differ, and arbitrarily bad things will happen, from getting
meaningless garbage to crashing your program.




标准明确地将其称为未定义行为。


来自6.3.2.3.8

"指向一种类型函数的指针可以转换为

a指向另一种类型的函数并再次返回;

结果应该等于原始指针。

如果转换指针用于调用一个函数的

类型与指向类型不兼容,

行为未定义。

-

Al Bowers

美国佛罗里达州坦帕市

mailto: xa ****** @ myrapidsys.com (删除x发送电子邮件)
http://www.geocities.com/abowers822/



The Standard explicitly calls it Undefined Behavior.

From 6.3.2.3.8
"A pointer to a function of one type may be converted to
a pointer to a function of another type and back again;
the result shall compare equal to the original pointer.
If a converted pointer is used to call a function whose
type is not compatible with the pointed-to type, the
behavior is undefined."
--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/


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

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