1

I came across a function call

    // call the entry point from the ELF header
    // note: does not return!
    ((void (*)(void)) (ELFHDR->e_entry & 0xFFFFFF))();

ELFHDR->e_entry is a function pointer. I think ((void (*)(void)) is the return type. But I'm not sure what the type void * void is.

Is there a rule to tell such type?

Thanks!

Mike
  • 1,841
  • 2
  • 18
  • 34
  • 2
    [spiral rule](http://c-faq.com/decl/spiral.anderson.html) (still had that link copied from last question as it happens). – chris Jan 12 '14 at 22:36
  • 3
    I like how you took the type and then randomly changed it for no reason, despite not knowing what it is or what it does... – Lightness Races in Orbit Jan 12 '14 at 22:39
  • 1
    "No reason" vs. not yet understanding that parens are significant to the meaning of type expressions and can't be removed from single elements...? It's the most basic mistakes that can be the most difficult. – Alex Celeste Jan 12 '14 at 22:43
  • 1
    Common sense dictates you don't make arbitrary changes in a question title just because you don't know what that might do. That's _even more reason_ to leave things well alone! – Lightness Races in Orbit Jan 12 '14 at 22:45
  • Then again, that's exactly what SO does to URLs, despite `(` being legal in URLs. – MSalters Jan 12 '14 at 23:28
  • I'm reading the code of bootloader of the mit os course. This line seems calling the first function in OS kernel when the bootloader finishes copying image from disk to RAM – Mike Jan 13 '14 at 03:23

3 Answers3

3
void (*)(void)

is the type pointer to a function with no parameter and that returns no value.

For example:

void foo(void)
{
}

void (*p)(void) = foo;  // p is of type void (*)(void)
ouah
  • 142,963
  • 15
  • 272
  • 331
2

The tool you want is cdecl, which translates C types into English. In this case, it translates:

(void (*)(void))

into:

cast unknown_name into pointer to function (void) returning void
David Given
  • 13,277
  • 9
  • 76
  • 123
2

You say that "I think ((void (*)(void)) is the return type" - it's not. It's casting the expression

(ELFHDR->e_entry & 0xFFFFFF)

to be a pointer to a function that takes no arguments and returns nothing.

The last () on the statement calls the function through that pointer.

Michael Burr
  • 333,147
  • 50
  • 533
  • 760