pyle
pyle

Reputation: 157

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!

Upvotes: 3

Views: 3051

Answers (5)

Caner Altinbasak
Caner Altinbasak

Reputation: 113

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

Upvotes: 0

t0mm13b
t0mm13b

Reputation: 34592

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

Upvotes: 3

Jérémie Koenig
Jérémie Koenig

Reputation: 1208

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

Upvotes: 1

abyx
abyx

Reputation: 72798

It means that the first argument of read, named Blub is a pointer to a function that returns a char * and receives no arguments.

Upvotes: 0

unwind
unwind

Reputation: 399813

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

Upvotes: 9

Related Questions