简体   繁体   English

类型化一个函数指针

[英]Typecasting a function pointer

I know this is a duplicate. 我知道这是重复的。 I've searched already but none address the problem I'm having. 我已经搜索过,但没有人解决我遇到的问题。 NOTE in an attempt to single out my confusion I have tried to simplify the typecast from the original. 注意 ,为了解决我的困惑,我试图简化原始的类型转换。 Hopefully it isn't undefined behaviour 希望它不是未定义的行为

Declaring a pointer to a function 声明指向函数的指针

Int (*funcPtr)(void*,void*);

Strcmp declaration Strcmp声明

int strcmp (char *, char *);

Typecasting a function pointer 类型化一个函数指针

FuncPtr = (int (*)(void*, void*))strcmp

This is where I get confused. 这是我感到困惑的地方。 I understand Typecasting a function pointer we have created. 我理解Typecasting我们创建的函数指针。 So type casting FuncPtr for example just changes what type of function funcPtr can point to. 因此,类型转换FuncPtr例如只是更改funcPtr可以指向的函数类型。 I have also read that the name of a function Is a function pointer to itself so Typecasting strcmp and using my understanding from above, 我还读过一个函数的名称是一个函数指针,所以Typecasting strcmp并使用我从上面的理解,

int (*)(void*, void*))strcmp 

Strcmp can now point to a function that takes two pointers to void and returns an int which Is clearly wrong as like arrays I think the function name can't be a pointer to another function. Strcmp现在可以指向一个函数,该函数接受两个void指针并返回一个明显错误的int,就像数组一样,我认为函数名不能是指向另一个函数的指针。

Because of my understanding above I fail to see how you can assign the address of strcmp to FuncPtr with the following typecast 由于我上面的理解,我没有看到如何使用以下类型转换将strcmp的地址分配给FuncPtr

FuncPtr = (int (*)(void*, void*))strcmp

As changing what the function pointer strcmp points to doesn't change the arguments and return type of strcmp . 作为改变什么函数指针的strcmp点不会改变参数和返回STRCMP类型。

I would really appreciate some knowledge I'm just so confused. 我真的很感激一些我很困惑的知识。

There are functions and there are function pointers. 有函数,还有函数指针。 strcmp is a function, linked to your project. strcmp是一个链接到项目的函数。 When the identifier strcmp is used in an expression, you get a function pointer to that function. 当在表达式中使用标识符strcmp ,您将获得指向该函数的函数指针。 But you cannot change "where strcmp points to" because strcmp is a function and not a pointer. 但你无法改变“strcmp指向的位置”,因为strcmp是一个函数,而不是一个指针。

C allows conversions between different function pointer types. C允许不同函数指针类型之间的转换。 However, should you try to call a function through a function pointer of incompatible type, you invoke undefined behavior. 但是,如果尝试通过不兼容类型的函数指针调用函数,则调用未定义的行为。

int (*funcPtr)(void*,void*);

is not compatible with strcmp , which has the following format: strcmp不兼容, strcmp具有以下格式:

int strcmp(const char *s1, const char *s2);

Meaning: 含义:

funcPtr = (int(*)(void*, void*))strcmp; // this is ok but not very meaningful
funcPtr(); // this is NOT ok, invokes undefined behavior

In order to actually call the function strcmp through that function pointer, you would have to cast back to the correct type, which is int (*)(const char*, const char*) . 为了通过该函数指针实际调用函数strcmp ,您必须转换回正确的类型,即int (*)(const char*, const char*)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM