Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does int (*func)() do in C?

Tags:

c

What does this line do in C?

 int (*func)();

I extracted it from the following program that is supposed to execute byte code (assembly instructions converted to its corresponding bytes)

char code[] = "bytecode will go here!";
int main(int argc, char **argv)
{
  int (*func)();
  func = (int (*)()) code;
  (int)(*func)();
}
like image 348
SivaDotRender Avatar asked Jun 17 '15 19:06

SivaDotRender


2 Answers

The line in question declares a function pointer for a function with unspecified arguments (an "obsolescent" feature since C99) and with return type int.

The first line of your main declares that pointer, the second line initializes the pointer so it points to the function code. The third line executes it.

You can get a description of pointers to functions in general here.

like image 198
mastov Avatar answered Sep 24 '22 05:09

mastov


int (*func)(); declares func as a pointer to a function that returns an int type and expects any number of arguments.

like image 38
haccks Avatar answered Sep 21 '22 05:09

haccks