Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does these two arguments mean in typedef?

I have a piece of code and I don't understand that one typedef:

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

Doesn't that actually mean you can use inst_cb_t as a void now? But what about the stuff in the second brackets?

like image 574
SimonH Avatar asked Feb 18 '15 15:02

SimonH


People also ask

What does typedef mean in C?

typedef is a reserved keyword in the programming languages C and C++. It is used to create an additional name (alias) for another data type, but does not create a new type, except in the obscure case of a qualified typedef of an array type where the typedef qualifiers are transferred to the array element type.

What is typedef example?

typedef struct { int scruples; int drams; int grains; } WEIGHT; The structure WEIGHT can then be used in the following declarations: WEIGHT chicken, cow, horse, whale; In the following example, the type of yds is "pointer to function with no parameter specified, returning int ".

What is a typedef statement?

A typedef declaration is a declaration with typedef as the storage class. The declarator becomes a new type. You can use typedef declarations to construct shorter or more meaningful names for types already defined by C or for types that you have declared.

What is the syntax of a typedef?

The syntax of typedef is as follows: Syntax: typedef data_type new_name; typedef : It is a keyword. data_type : It is the name of any existing type or user defined type created using structure/union.


1 Answers

The 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 use this only in function declaration and pointer to function deceleration.

inst_cb_t foo;  

You can't use it in function definition like

inst_cb_t foo      // WRONG
{
    // Function body
}  

Look at C standard:

C11: 6.9.1 Function definitions:

The identifier declared in a function definition (which is the name of the function) shall have a function type, as specified by the declarator portion of the function definition.162)

and footnote 162 is

The intent is that the type category in a function definition cannot be inherited from a 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
like image 64
haccks Avatar answered Sep 29 '22 03:09

haccks