c - typedef a function interface (not function pointer) -


if typedef this:

typedef int (read_proc_t)(char *page, char **start, off_t off,                           int count, int *eof, void *data); 

as defined here

what technique called?

if typedef way, means create type read_proc_t; later, create own version of read-proc-t, my_read_proc , have struct this:

struct test {     read_proc_t read_proc; }   struct test t; 

i can this:

t.read_proc_t = my_read_proc; 

is correct? normally, if typedef read_proc_t function pointer:

typedef int (*read_proc_t)(char *page, char **start, off_t off,                           int count, int *eof, void *data); 

i have assign my_read_proc function address function pointer, this:

(*t.read_proc_t) = &my_read_proc; 

in circumstance choose one?

the difference in first case type read_proc_t not implicit pointer semantic, while in second case becomes pointer type.

the difference between 2 variables of first type cannot created in contexts. example, illegal define struct this:

struct test {     // causes error in c99:     // field ‘read_proc’ declared function     read_proc_t read_proc; }; 

you need asterisk make valid:

struct test {     read_proc_t *read_proc; }; 

however, read_proc_t can used declare function parameters:

void read_something(read_proc_t rd_proc) {     rd_proc(...); // invoke function } 

there degree of interchangeability between read_proc_t , read_proc_t*: example, can call above function this:

read_proc_t *rd = my_read_01; read_something(rd); // <<== read_proc_t* passed read_proc_t 

in general, should prefer first declaration keep function pointer semantic explicit, because better readability. if use second declaration (with asterisk) should name type in way tells reader pointer, example, this:

typedef int (*read_proc_ptr_t)(char *page, char **start, off_t off,                       int count, int *eof, void *data); 

Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -