类型转换函数指针 [英] typecasting of function pointers

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

问题描述



可以将一个函数指针强制转换为两个不同的原型。

例如。

typedef void(functptr1 *)(int,int);

typedef void(functptr2 *)(int);

functptr1 fptr;

fptr = somefunction_name;

fptr(10,20);

fptr =(functptr2)someotherfunction_name;

(functptr2)fptr(10 );


我尝试了以上它没有工作。如果这是错误的,请告诉我

如何以正确的方式完成,如果它是特定于编译器的,

哪个编译器允许这样做。

解决方案

sr ************* @ gmail.com schrieb:


是否有可能将函数指针强制转换为两个不同的
原型。
例如,
typedef void(functptr1 *)(int,int);
typedef void(functptr2 *)(int);
functptr1 fptr;
fptr = somefunction_name;
fptr(10,20);
fptr =(functptr2)someotherfunction_name;
(functptr2)fptr(10);

我尝试了以上它没有工作。如果这是错误的,请告诉我
如何以正确的方式完成,如果它是特定于编译器的,
哪个编译器允许这样做。




请详细说明什么都行不通意味着将来。

看看

< http://www.catb.org/~esr/faqs/smart-questions.html>

并提供_actual_代码。

以上无法编译。


因此,对于语言设施:

1.函数指针不能隐式转换(即没有强制转换的
)。它们不能明确地(即通过强制转换)

到另一个函数指针类型的便携式,标准的

这意味着:每当你将一个函数指针类型转换为另一个函数指针类型

,你的程序本身就变得不便携了

甚至在你使用另一个版本的
$ b $时可能会中断b相同的编译器。

2.如果你有相同的返回类型,你有很好的机会

它仍然可以工作;正确的方法来完成上述



void foo(int qux,int quux);

void bar(int baz);


fptr = foo;

fptr(10,20);

fptr =(functptr1)bar;

((functptr2)fptr)(10);

,其中包含typedef和声明。

但是,如果调用错误,你可能遇到深深的麻烦br />
具有错误参数数量的函数类型,所以这个

充其量只是一个坏主意。如果你需要不同的指针类型,

考虑使用函数指针的联合和某种类型的

类型标记。

注意:如果你的参数list有很多参数(比如> 5),

然后机会增加它不起作用。

3.在许多实现中,甚至在函数之间转换

不同返回类型的指针类型可能有效。


干杯

Michael

-

电子邮件:我的是/ at / gmx / dot / de地址。


Michael Mair写道:

sr ************* @ gmail.com schrieb:< blockquote class =post_quotes>
是否可以将函数指针强制转换为两个不同的原型。
例如。
typedef void(functptr1 *)(int, int);
typedef void(functptr2 *)(int);
functptr1 fptr;
fptr = somefunct ion_name;
fptr(10,20);
fptr =(functptr2)someotherfunction_name;
(functptr2)fptr(10);

我试过以上它做了不行。如果这是错误的,请告诉我
如何以正确的方式完成,如果它是特定于编译器的,那么哪个编译器允许这样做。
请准确说明什么没有工作意味着将来。
看看
< http://www.catb.org/~esr/faqs/smart-questions.html>
并提供_actual_代码。<以上无法编译。

因此,对于语言设施:
1.函数指针不能隐式转换(即没有强制转换)。它们不能以便携式,标准的方式显式地(即通过强制转换)
到另一个函数指针类型。
这意味着:每当你将一个函数指针类型转换为另一个时,你的程序本质上是不可移植的,甚至可能在你使用另一个版本的相同编译器时中断。




不完全正确。从一个函数

类型转换到另一个函数是完全合法的,只需要在

调用它之前再次强制转换它。甚至有时这很有用,虽然不是很经常。
2.

2.如果你有相同的回报类型,你有很好的机会
它尽管如此;正确的做法是

void foo(int qux,int quux);
void bar(int baz);

fptr = foo;
fptr(10,20);
fptr =(functptr1)bar;
((functptr2)fptr)(10);
带有typedef和声明。
但是,如果使用错误数量的参数调用错误的函数类型,则会遇到深深的麻烦,所以这个
充其量只是一个坏主意。如果你需要不同的指针类型,
考虑使用函数指针和某种
类型标记的联合。
注意:如果你的参数列表有很多参数(比如> 5),<然后机会增加,它将无法正常工作。


我强烈建议不要这样做,即使它似乎有效。

如果你不总是使用所有的参数那么有两个简单的

处理它的方法:

1)为未使用的

参数传递合适的0值(或其他一些位置标记)。

2)编写一个函数,使用

机制C为此提供可变数量的参数。

3.在许多实现中,甚至在函数之间进行转换
不同返回类型的指针类型可能有效。




但是,即使它有效,你仍然不应该这样做。

-

Flash Gordon,生活在有趣的时代。

网站 - http://home.flash-gordon.me.uk/

comp.lang.c发布指南和介绍:
http://clc-wiki.net / wiki / Intro_to_clc


Flash Gordon schrieb:

Michael Mair写道:

sr ************* @ gmail.com schrieb:


是否可以将函数指针强制转换为两个不同的原型。
例如。
typedef void(functptr1 *) (int,int);
typedef void(functptr2 *)(int);
functptr1 fptr;
fptr = somefunction_name;
fptr(10,20);
fptr =(functptr2)someotherfunction_name;
(functptr2)fptr(10);

我尝试了以上它没有工作。如果这是错误的,请告诉我
如何以正确的方式完成,如果它是特定于编译器的,
哪个编译器允许这样做。


<请详细说明什么都行不通。意味着将来。
看看
< http://www.catb.org/~esr/faqs/smart-questions.html>
并提供_actual_代码。<以上无法编译。

因此,对于语言设施:
1.函数指针不能隐式转换(即没有强制转换)。它们不能以便携式,标准的方式显式地(即通过强制转换)
到另一个函数指针类型。
这意味着:每当你将一个函数指针类型转换为另一个时,你的程序本质上是不可移植的,甚至可能在你使用另一个版本的
编译器时中断。



不完全正确。从一个函数类型转换到另一个函数是完全合法的,只需要在调用它之前再次将其强制转换。甚至有时候这很有用,虽然不是很经常。




感谢您指出这一点。

记录:C99,6.3.2.3#8

2.如果你有相同的返回类型,你很有可能
它仍将起作用;正确的做法是

void foo(int qux,int quux);
void bar(int baz);

fptr = foo;
fptr(10,20);
fptr =(functptr1)bar;
((functptr2)fptr)(10);
带有typedef和声明。
但是,如果使用错误数量的参数调用错误的函数类型,则会遇到深深的麻烦,所以这个
充其量只是一个坏主意。如果你需要不同的指针类型,
考虑使用函数指针和某种
类型标记的联合。
注意:如果你的参数列表有很多参数(比如> 5),<然后机会增加,它将无法工作。



我强烈建议不要这样做,即使它似乎有用。
如果你不总是使用所有参数然后有两种简单的方法来处理它:
1)为未使用的
参数传递合适的0值(或其他一些位置标记)。
2)写一个使用
机制C获取可变数量参数的函数提供了这样做。




注意:前者可能不是自然而然的事情;有时基于工会的方法更有帮助。后者,特别是带键/值对的
,不会受到影响,但可能会造成麻烦的参数组合检查。

3.在许多实现中,甚至可以在不同返回类型的函数
指针类型之间进行转换。



然而,即使它你不应该这样做。




确实:-)

干杯

Michael

-

电子邮件:我的是/ at / gmx / dot / de地址。


Hi,
is it possible to typecast a function pointer to two different
prototypes.
eg.,
typedef void (functptr1 *) (int , int);
typedef void (functptr2 *) (int);
functptr1 fptr;
fptr = somefunction_name;
fptr(10,20);
fptr = (functptr2)someotherfunction_name;
(functptr2) fptr(10);

I tried the above it did''nt work. If this is wrong please let me know
how it could be done in the right way, also if it is compiler specific,
which compiler(s) permit this.

解决方案

sr*************@gmail.com schrieb:

Hi,
is it possible to typecast a function pointer to two different
prototypes.
eg.,
typedef void (functptr1 *) (int , int);
typedef void (functptr2 *) (int);
functptr1 fptr;
fptr = somefunction_name;
fptr(10,20);
fptr = (functptr2)someotherfunction_name;
(functptr2) fptr(10);

I tried the above it did''nt work. If this is wrong please let me know
how it could be done in the right way, also if it is compiler specific,
which compiler(s) permit this.



Please, state exactly what "didn''t work" means in future.
Have a look at
<http://www.catb.org/~esr/faqs/smart-questions.html>
And provide the _actual_ code.
The above cannot compile.

So, for the language facilities:
1. Function pointers cannot be converted implicitly (i.e.
without cast). They cannot be explicitly (i.e. via cast)
to another function pointer type in a portable, standard
C way.
This means: Whenever you cast one function pointer type
to another, your program becomes inherently non-portable
and may even break when you use another version of the
same compiler.
2. If you have the same return type, you have good chances
that it will work nonetheless; the right way to do the above
is
void foo(int qux, int quux);
void bar(int baz);

fptr = foo;
fptr(10,20);
fptr = (functptr1) bar;
((functptr2)fptr)(10);
with the typedefs and declarations in place.
However, you can run into deep trouble if calling the wrong
type of function with the wrong number of arguments, so this
is a bad idea at best. If you need different pointer types,
consider using a union of function pointers and some kind of
type marker.
Note: If your parameter list have many parameters (say >5),
then the chance increases that it will not work.
3. On many implementations, even casts between function
pointer types for different return types may work.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.


Michael Mair wrote:

sr*************@gmail.com schrieb:

Hi,
is it possible to typecast a function pointer to two different
prototypes.
eg.,
typedef void (functptr1 *) (int , int);
typedef void (functptr2 *) (int);
functptr1 fptr;
fptr = somefunction_name;
fptr(10,20);
fptr = (functptr2)someotherfunction_name;
(functptr2) fptr(10);

I tried the above it did''nt work. If this is wrong please let me know
how it could be done in the right way, also if it is compiler specific,
which compiler(s) permit this.
Please, state exactly what "didn''t work" means in future.
Have a look at
<http://www.catb.org/~esr/faqs/smart-questions.html>
And provide the _actual_ code.
The above cannot compile.

So, for the language facilities:
1. Function pointers cannot be converted implicitly (i.e.
without cast). They cannot be explicitly (i.e. via cast)
to another function pointer type in a portable, standard
C way.
This means: Whenever you cast one function pointer type
to another, your program becomes inherently non-portable
and may even break when you use another version of the
same compiler.



Not completely true. It is perfectly legal to cast from one function
type to another, it''s just that you have to cast it back again before
calling it. There are even times when this is useful, although not very
often.
2. If you have the same return type, you have good chances
that it will work nonetheless; the right way to do the above
is
void foo(int qux, int quux);
void bar(int baz);

fptr = foo;
fptr(10,20);
fptr = (functptr1) bar;
((functptr2)fptr)(10);
with the typedefs and declarations in place.
However, you can run into deep trouble if calling the wrong
type of function with the wrong number of arguments, so this
is a bad idea at best. If you need different pointer types,
consider using a union of function pointers and some kind of
type marker.
Note: If your parameter list have many parameters (say >5),
then the chance increases that it will not work.
I would strongly advise against doing this even if it appears to work.
If you do not always use all the parameters then there are two simple
ways to deal with it:
1) Pass suitable 0 values (or some other place marker) for unused
parameters.
2) Write a function that takes a variable number of arguments using the
mechanism C provides for doing this.
3. On many implementations, even casts between function
pointer types for different return types may work.



However, even if it works you should still not do it.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc


Flash Gordon schrieb:

Michael Mair wrote:

sr*************@gmail.com schrieb:

Hi,
is it possible to typecast a function pointer to two different
prototypes.
eg.,
typedef void (functptr1 *) (int , int);
typedef void (functptr2 *) (int);
functptr1 fptr;
fptr = somefunction_name;
fptr(10,20);
fptr = (functptr2)someotherfunction_name;
(functptr2) fptr(10);

I tried the above it did''nt work. If this is wrong please let me know
how it could be done in the right way, also if it is compiler specific,
which compiler(s) permit this.



Please, state exactly what "didn''t work" means in future.
Have a look at
<http://www.catb.org/~esr/faqs/smart-questions.html>
And provide the _actual_ code.
The above cannot compile.

So, for the language facilities:
1. Function pointers cannot be converted implicitly (i.e.
without cast). They cannot be explicitly (i.e. via cast)
to another function pointer type in a portable, standard
C way.
This means: Whenever you cast one function pointer type
to another, your program becomes inherently non-portable
and may even break when you use another version of the
same compiler.



Not completely true. It is perfectly legal to cast from one function
type to another, it''s just that you have to cast it back again before
calling it. There are even times when this is useful, although not very
often.



Thanks for pointing that out.
For the record: C99, 6.3.2.3#8

2. If you have the same return type, you have good chances
that it will work nonetheless; the right way to do the above
is
void foo(int qux, int quux);
void bar(int baz);

fptr = foo;
fptr(10,20);
fptr = (functptr1) bar;
((functptr2)fptr)(10);
with the typedefs and declarations in place.
However, you can run into deep trouble if calling the wrong
type of function with the wrong number of arguments, so this
is a bad idea at best. If you need different pointer types,
consider using a union of function pointers and some kind of
type marker.
Note: If your parameter list have many parameters (say >5),
then the chance increases that it will not work.



I would strongly advise against doing this even if it appears to work.
If you do not always use all the parameters then there are two simple
ways to deal with it:
1) Pass suitable 0 values (or some other place marker) for unused
parameters.
2) Write a function that takes a variable number of arguments using the
mechanism C provides for doing this.



Note: The former may not the natural thing to do; sometimes the
union-based approach is more helpful. The latter, especially
with key/value pairs, does not suffer from that but may make for
troublesome parameter combination checking.

3. On many implementations, even casts between function
pointer types for different return types may work.



However, even if it works you should still not do it.



Indeed :-)
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.


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

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