简体   繁体   中英

What's the meaning of typedef int function(void*)?

I saw some BSD code using the following construct:

typedef int driver_filter_t(void*);

What does that mean, exactly? I don't think it's a function pointer because otherwise it would be something like typedef int (*driver_filter_t)(void*) , right?

typedef int driver_filter_t(void*);

This is a definition of a function type . It makes driver_filter_t an alias for the type that can be described as "function returning int with an argument of type pointer to void ".

As for all typedef s, it creates an alias for an existing type, not a new type.

driver_filter_t is not a pointer type. You can't declare something of type driver_filter_t (the grammar doesn't allow declaring a function using a typedef name). You can declare an object that's a function pointer as, for example:

driver_filter_t *func_ptr;

Because you can't use a function type name directly without adding a * to denote a pointer type, it's probably more common to define typedef s for function pointer types, such as:

typedef int (*driver_filter_pointer)(void*);

But typedefs for function types are pefectly legal, and personally I find them clearer.

typedef int driver_filter_t(void*); is a typedef for a function type. In C you can use it for function pointers like driver_filter_t* fn_ptr .

In C++ you can also use that typedef to declare member functions (but not to implement them):

struct Some {
    driver_filter_t foo; // int foo(void*);
    driver_filter_t bar; // int bar(void*);
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM