DEV Community

Paul J. Lucas
Paul J. Lucas

Posted on • Updated on

Function types in C (and C++)

Even after programming in C for decades, I occasionally still discover things about it. While typedefs of pointer to function are common, I just discovered that you can have typedefs of function by stumbling across their use in some open-source project (though I don't recall which one). That is instead of the common:

typedef void (*PF)(int);
PF pf;                    // pf is a pointer to function
Enter fullscreen mode Exit fullscreen mode

you can instead do:

typedef void F(int);
F *pf;                   // pf is (also) a pointer to function
Enter fullscreen mode Exit fullscreen mode

One advantage the latter is that it makes it more obvious that you're dealing with pointers.

I suppose the reason for not knowing about typedefs of function is that there are no such examples in either the first or second editions of The C Programming Language (though there are examples of typedefs of pointer to function). There is, however, an example in the C99 standard §6.7.7.4:

After

    typedef int MILES, KLICKSP();

the constructions

    extern KLICKSP *metricp;

are all valid declarations. The type of ... metricp is ‘‘pointer to function with no parameter specification returning int’’ ....

Discussion (5)

Collapse
carnato profile image
Info Comment hidden by post author - thread only accessible via permalink
Carnato

overriding can help me redefine the definition of the base class function
then why we required the virtual keyword because it also help me redefine the function
then plz help me to solve the contraversy

Collapse
pauljlucas profile image
Paul J. Lucas Author

No. This post isn't the place for that.

Collapse
carnato profile image
Carnato

Then how and where

Thread Thread
pauljlucas profile image
Paul J. Lucas Author

Here comes to mind.

Collapse
carnato profile image
Info Comment hidden by post author - thread only accessible via permalink
Carnato

can you explain me what is overriding and what is difference b/w overriding and virtual function
and also what is the use of the virtual function

Some comments have been hidden by the post's author - find out more