4

What is the difference between

extern int (*func)(void);

and

extern int *func(void);

Thanks

gnued
  • 81
  • 5
  • 3
    One is a function pointer, and the other is a function returning an int pointer. – PiRocks Feb 28 '20 at 11:43
  • 1
    @PiRocks You could post that as the answer to the question. No more details are needed, really. – Lundin Feb 28 '20 at 11:55
  • I guess this is more a off topic question for meta than for the comments, but this question seems like a duplicate to me. I was unable to quickly find what it is a duplicate of quickly though. So like it's a duplicate but not. So should it be flagged? – PiRocks Feb 28 '20 at 14:23
  • 1
    Seeing https://stackoverflow.com/questions/14114749/c-function-pointer-syntax/ and https://softwareengineering.stackexchange.com/questions/117024/why-was-the-c-syntax-for-arrays-pointers-and-functions-designed-this-way/ I would also say it's a duplicate and should NOT be used as a ("looks OK") audit question! – B. Go Mar 10 '20 at 19:49

3 Answers3

7
extern int (*func)(void);

declares func as a pointer to a function which takes no arguments and returns an int value.

extern int *func(void);

is a forward declaration (a.k.a. a protptype) of func as a function that takes no arguments and returns a pointer to an int.

The first declares a variable, the second declares a function.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
2

difference between int (*cmp)(void) and int *cmp(void)?

Use C gibberish ↔ English 1

Pointer Declaration
extern int (*cmp)(void); is a extern pointer to function (void) returning int.

Function Declaration
extern int *cmp(void); is a extern function (void) returning pointer to int.


1 "func" is a keyword there, use `"cmp".

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
1

If you declare a type fp as pointer to a function, that the compiler will interpret fp() as dereferencing fp to get the address of the function.

Whereas, if fp is declared as the function itself, then any fp() in your file, will be interpret by the compiler as a near-call to the address of fp.

Meaning, that the linker will fix up the jump as the offset between the caller address, and the address of fp itself.

Not the address located at fp, but the offset to fp.