-1

I've been meaning to ask this question for a while now. What's going on with these functions? Why are the names in parenthesis?

void        (*think)(gentity_t *self);
void        (*reached)(gentity_t *self);    // movers call this when hitting endpoint
void        (*blocked)(gentity_t *self, gentity_t *other);
void        (*touch)(gentity_t *self, gentity_t *other, trace_t *trace);
LJD
  • 498
  • 4
  • 11
  • 2
    These are function pointers. Have you encountered them before? – templatetypedef Aug 18 '17 at 00:56
  • 1
    Not really, I'm mostly using C for a mod project. I get a lot of the syntax, and understand how to use pointers/allocate memory, but some of the pointer syntax is confusing. Feels a lot like a convoluted inside-joke at times. Guess it's time to buy some advanced books. – LJD Aug 18 '17 at 01:12
  • 1
    Guessing there is an array of each of these types. Guessing that each "Actor" registers its own function to this array. Guessing the array is traversed and each element is "run" once so that every "actor" has its own variation of a run function executed. Poor man's polymorphism here. – Michael Dorgan Aug 18 '17 at 01:27
  • But is it bad polymorphism, or just not polymorphism at all? – LJD Aug 18 '17 at 02:25
  • 1
    The parentheses are needed for the `*` so that `*` binds to the identifier `think`, `react`, etc; if there were no parentheses these would declare functions with return value of type `void *`. – Antti Haapala -- Слава Україні Aug 18 '17 at 04:58

3 Answers3

3

In your examples, the parenthesis in function name means that variable of pointing the function address. If you don't use the parenthesis

void * think(gentity_t *self);// equal (void *) think(gentity_t *self); 

It means the definition of a function with name:think, return: void *, parameter: gentity_t *self; These are the variable of the pointing the functions.

R.Mirana
  • 68
  • 9
  • Did you mean "equal to void (*think)(gentity_t *self);" or are all 3 of these statements the same? – LJD Aug 18 '17 at 01:12
  • 1
    No I mean. It's absolutely different void (*think)(...) and void * think(...); First case is the definition of function and sec is definition of variable of function's pointer. – R.Mirana Aug 18 '17 at 01:14
  • Oh, so the parenthesis is the only way that you'd be able to determine it was a function pointer and not a function that returns a pointer? What about (void * ) (*think)(gentity_t *self); ? Is that a pointer function that returns a pointer? – LJD Aug 18 '17 at 01:54
  • 1
    Yes, that's right – R.Mirana Aug 18 '17 at 01:55
  • 2
    While correct in principle, the wording is quite incorrect. If you don't use the parentheses, you have a function *declaration*; if you have the parentheses, you don't have a *function*, but a definition (possibly tentative) for an object of type pointer-to-function. – Antti Haapala -- Слава Україні Aug 18 '17 at 04:54
  • Notice also that the *parentheses* don't do that but the `*`. https://stackoverflow.com/questions/13600790/what-do-the-parentheses-around-a-function-name-mean – Antti Haapala -- Слава Україні Aug 18 '17 at 04:57
  • parentheses in this link doesn't essential for the definition of function. – R.Mirana Aug 18 '17 at 07:56
2

These declarations are function pointers, which point to a function and can be changed at any time.

I suggest you do some research on function pointers in C because they are very useful.

If you know C++'s std::function then these are effectively the old C version of them.

flmng0
  • 387
  • 1
  • 2
  • 14
1

These are function pointers and not the function names. So they can point to any function of same type and properties.

Milind Deore
  • 2,887
  • 5
  • 25
  • 40