What do these two arguments mean in typedef?

I have a piece of code, and I don’t understand what one typedef :

 typedef void (inst_cb_t) (const char*, size_t); 

Does this mean that now you can use inst_cb_t as void ? But what about the material in the second brackets?

+6
source share
1 answer

Declaration

 typedef void (inst_cb_t) (const char*, size_t); 

defines inst_cb_t as a function that takes two arguments of type const char* and size_t and returns void .

One interesting part of this declaration is that you can only use this in a function declaration and a pointer to slowing down a function.

 inst_cb_t foo; 

You cannot use it in a function definition like

 inst_cb_t foo // WRONG { // Function body } 

Take a look at standard C:

C11: 6.9.1 Function definitions:

The identifier declared in the function definition (which is the name of the function) must be of the function type, as indicated in the declaration part of the function definition. 162)

and footnote 162 -

The intention is that the type category in a function definition cannot be inherited from typedef :

 typedef int F(void); // type F is ''function with no parameters // returning int F f, g; // f and g both have type compatible with F F f { /* ... */ } // WRONG: syntax/constraint error F g() { /* ... */ } // WRONG: declares that g returns a function int f(void) { /* ... */ } // RIGHT: f has type compatible with F int g() { /* ... */ } // RIGHT: g has type compatible with F F *e(void) { /* ... */ } // e returns a pointer to a function F *((e))(void) { /* ... */ } // same: parentheses irrelevant int (*fp)(void); // fp points to a function that has type F F *Fp; // Fp points to a function that has type F 
+12
source

Source: https://habr.com/ru/post/1213624/


All Articles