5

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)();
}
SivaDotRender
  • 1,581
  • 4
  • 21
  • 37

2 Answers2

5

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.

mastov
  • 2,942
  • 1
  • 16
  • 33
  • @haccks: Edited. I had confused it with `void` as argument specification. – mastov Jun 17 '15 at 20:01
  • @haccks: No downvote, but withdrawal. I'm more of a C++ guy, I should watch out better for the differences when answering C questions. – mastov Jun 17 '15 at 20:06
  • obsolescent? do you mean obsolete? or do you actually mean that its been becoming more obsolete since C99? – Steve Cox Jun 17 '15 at 20:40
  • @SteveCox: http://dictionary.reference.com/browse/obsolescent and http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf (6.11.6, p. 179) – mastov Jun 17 '15 at 20:44
  • @SteveCox: Apparently it's their way of saying "deprecated": *Certain features are obsolescent, which means that they may be considered for withdrawal in future revisions of this International Standard. They are retained because of their widespread use, but their use in new implementations (for implementation features) or new programs (for language [6.11] or library features [7.31]) is discouraged.* (page XVII) – mastov Jun 17 '15 at 21:06
3

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

haccks
  • 104,019
  • 25
  • 176
  • 264
  • The arguments can be any type, not just int. No conversions(other than the normal function argument promotions) or checking will be done on any arguments passed to the function when it is called, but if they don't match what the function is expecting, the result is undefined. – Chris Dodd Jun 17 '15 at 20:29
  • @ChrisDodd; Agreed. Removed that line, but many C compilers assumes default type to be `int`. – haccks Jun 17 '15 at 20:32
  • The defaulting to `int` only applies to declarators with no type specifier. So if you said `int (*func)(a)`, that would be one argument of type `int` named `a`. Or if you said `(*func)();` in the global scope, the return type would default to `int` -- defaulting to `int` only occurs in scopes where expressions are not possible (so not on local vars within a function). – Chris Dodd Jun 17 '15 at 20:36
  • 1
    @ChrisDodd - to be more precise, arguments will undergo default argument promotion. More details for the curious are here: http://stackoverflow.com/questions/1255775/default-argument-promotions-in-c-function-calls I don't mean to be pedantic, but such argument promotion can cause hard-to-diagnose problems, with this particular example being prone to such issues. – Andrew Henle Jun 17 '15 at 20:39