简体   繁体   English

(int(*)())在函数调用中的含义

[英]What does (int (*)()) mean in a function call

Like the title says, I want to know what " (int (*)()) " in a C-define-function-call means? 就像标题所说,我想知道C-define-function-call中的“ (int(*)()) ”是什么意思?

As example, it looks similar to this: 例如,它看起来类似于:

#define Bla(x)    (Char *) read((char *(*)()) Blub, (char **) x)

or this 或这个

#define XXX(nx, id)     PEM_ASN1_write_bio((int (*)()) id, (char *) nx)

Thank you in advance! 先感谢您!

The casts the argument to a pointer to a function that returns char * and takes zero or more arguments. 将参数转换为指向函数的指针,该函数返回char *并接受零个或多个参数。 The second function returns int . 第二个函数返回int

You can use a program (and website, now) called " cdecl " to help with these, it says: 您可以使用名为“ cdecl ”的程序(现在称为“ cdecl ”)来帮助解决这些问题,它说:

  • (char *(*)()) : cast unknown_name into pointer to function returning pointer to char (char *(*)()) :将unknown_name转换为指向函数的指针,返回指向char的指针
  • (int (*)()) : cast unknown_name into pointer to function returning int (int (*)()) :将unknown_name转换为函数返回int的指针

The easiest way of deciphering complex C expressions is to start with the innermost expression, then in an anti-clockwise pattern move on to the next. 解密复杂C表达式的最简单方法是从最内层表达式开始,然后以逆时针方向图案移动到下一个表达式。 (int (*)()) (int(*)())

  1. (*) A Pointer, anti-clockwise motion hits on (, then move on again to hit on ) (*)指针,逆时针运动点击(然后再次移动以打开)
  2. (*)() A pointer to function, move on to (, then move on again to ) (*)()指向函数的指针,继续执行(然后再次移动到)
  3. int (*)() A pointer to a function returning int, move on to int, then move on to hit on ) int(*)()一个返回int的函数的指针,移到int,然后继续点击)
  4. (int (*)()) finally move on to (, and there you have it, (int(*)())最后转到(,你有它,

A pointer to a function returning int, since it is wrapped in the outer () is because of the macro. 返回int的函数的指针,因为它包含在outer()中是因为宏。

Hope this helps, Best regards, Tom 希望这会有所帮助,最好的问候,汤姆

(int (*)()) is a typecast operator, which is to say you ask the compiler to behave as if the expression on its right were of type int (*)() . (int (*)())是一个类型转换运算符,也就是说你要求编译器的行为就像它右边的表达式是int (*)() As others have indicated, the type in question means "a pointer to a function accepting any arguments, and returning an int ". 正如其他人所指出的那样,所讨论的类型意味着“指向接受任何参数的函数的指针,并返回一个int ”。

To understand the type itself, you first need to understand the weird way in which variables are declared in C: in most languages, the syntax for variable declarations is constructed from the syntax for type specifications, but in C, in a way, it's the other way around. 要理解类型本身,首先需要理解在C中声明变量的奇怪方式:在大多数语言中,变量声明的语法是从类型规范的语法构造的,但在C中,在某种程度上,它是其他方式。

If you were to declare a variable containing a pointer to such a function, you would write: 如果你要声明一个包含指向这样一个函数的指针的变量,你会写:

int (*fp)();

meaning that an expression resembling (*fp)() would be of type int : "take fp , dereference it, call that with any arguments and you will get an int ". 意思是类似于(*fp)()的表达式将是int类型:“取fp ,取消引用它,用任何参数调用它,你将得到一个int ”。

Now, in order to obtain a typecast operator for the type of fp in the above declaration, lose the identifier and add parentheses around: you get (int (*)()) . 现在,为了在上面的声明中获得fp类型的类型转换运算符,丢失标识符并在括号周围添加括号:get get (int (*)())

这意味着名为Blubread的第一个参数是一个指向函数的指针,该函数返回一个char *并且不接收任何参数。

First one says that read takes "a function pointer that returns a char pointer" as first argument and "pointer to a char pointer" as second argument. 第一个说,read将“返回char指针的函数指针”作为第一个参数,将“指向char指针的指针”作为第二个参数。 If you want to do Bla, just write Bla(x), I ll handle de read part! 如果你想做Bla,只需写Bla(x),我就会处理de read部分!

Second one says that, first parameter to PEM_ASN1_write_bio must be "a function pointer returning an int". 第二个说,PEM_ASN1_write_bio的第一个参数必须是“返回int的函数指针”。 And the second argument is "a pointer to a char". 第二个参数是“指向char的指针”。 And you can use XXX(a,b) instead of PEM_ASN1_write_bio(b,a), thats all 你可以使用XXX(a,b)代替PEM_ASN1_write_bio(b,a),这就是全部

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

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