简体   繁体   中英

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?

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. The second function returns int .

You can use a program (and website, now) called " cdecl " to help with these, it says:

  • (char *(*)()) : cast unknown_name into pointer to function returning pointer to char
  • (int (*)()) : cast unknown_name into pointer to function returning 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. (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 )
  4. (int (*)()) finally move on to (, and there you have it,

A pointer to a function returning int, since it is wrapped in the outer () is because of the macro.

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 (*)() . As others have indicated, the type in question means "a pointer to a function accepting any arguments, and returning an 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.

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 ".

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 (*)()) .

这意味着名为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. If you want to do Bla, just write Bla(x), I ll handle de read part!

Second one says that, first parameter to PEM_ASN1_write_bio must be "a function pointer returning an int". And the second argument is "a pointer to a char". And you can use XXX(a,b) instead of PEM_ASN1_write_bio(b,a), thats all

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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