Using a defined function type in a function declaration

96 Views Asked by At

I defined a pointer to a function type:

 typedef uint32_t (*funct_t)(bool);

I declared a function that uses the same interface:

uint32_t a_funct(bool);

I need to use the pointer to a function type instead. How can I do that?

2

There are 2 best solutions below

0
On BEST ANSWER

If you define the typedef like this:

typedef uint32_t (funct_t)(bool);   // note the missing * before the type name

You can declare the function like this:

funct_t a_funct;

Note that when you define the function, you still need to spell out the parameters.

uint32_t a_funct(bool x)
{
    ...
}
1
On

I defined a function type:

typedef uint32_t (*funct_t)(bool);

No, you did not declare a function. You declared pointer to a function type.

To declare a function type you could write

typedef uint32_t funct_t(bool);

and then you can use the type to declare the function a_funct

funct_t  a_funct;

Take into account that you may not use a typedef of a function type to define a function.