KeithSmith
KeithSmith

Reputation: 774

Old C code Notation for a Function that Returns a Function Pointer

I am looking at some late 80's C code. I have the following definition.

void (*_flush())(){ /* body ommitted */ }

I believe this is a function, _flush , with void as its argument list and returns a function pointer that returns void and has void as its argument list.

The equivalent in two parts:

typedef void (*funcptr)(void);
functptr flush(void){  /* body ommitted */ }

Do I understand the old notation correctly?

Upvotes: 2

Views: 145

Answers (2)

chux
chux

Reputation: 154146

Use http://www.cdecl.org/ to get an nice translation of your function.

void (*_flush())()
declare _flush as function returning pointer to function returning void

Had the definition of function _flush() used any parameters itself, they would have been listed between the last ) and { so we can be confident that the function _flush takes no arguments.

void (*_flush())() { /* body omitted */ }
//----------------^

The return value of flush is itself a function, let's call it foo

void foo(...)
declare foo as function returning void

Here it is known that the function returns void, but its parameter list could be of any number of parameters and of any number of any type. Just not known from the info given but likely discernible from the body of _flush.

Upvotes: 0

Carl Norum
Carl Norum

Reputation: 225112

Yes, your understanding is (almost) correct:

cdecl> explain void (*_flush())()
declare _flush as function returning pointer to function returning void

It's not correct to say that your function has void as its argument list, though. A function like:

void func()

Doesn't take no arguments, it takes an unspecified number of arguments. You could call it in whichever of these ways is appropriate:

func()
func(a);
func(a, b, c);

and so on. If the implementation of _flush doesn't make use of any parameters, you can probably safely use void as you suggest, but your example doesn't include enough information to say.

A good reference: C function with no parameters behavior

Upvotes: 2

Related Questions