Monday 15 March 2010

c - Understanding snippet of code -



c - Understanding snippet of code -

im reading book , there no online code reference don't understand couple of things. finish code:

#include <stdio.h> /* api implement */ struct greet_api { int (*say_hello)(char *name); int (*say_goodbye)(void); }; /* our implementation of hello function */ int say_hello_fn(char *name) { printf("hello %s\n", name); homecoming 0; } /* our implementation of goodbye function */ int say_goodbye_fn(void) { printf("goodbye\n"); homecoming 0; } /* struct implementing api */ struct greet_api greet_api = { .say_hello = say_hello_fn, .say_goodbye = say_goodbye_fn }; /* main() doesn't need know how * say_hello/goodbye works, knows */ int main(int argc, char *argv[]) { greet_api.say_hello(argv[1]); greet_api.say_goodbye(); printf("%p, %p, %p\n", greet_api.say_hello, say_hello_fn, &say_hello_fn); exit(0); }

i have never seen before:

int (*say_hello)(char *name); int (*say_goodbye)(void);

and:

struct greet_api greet_api = { .say_hello = say_hello_fn, .say_goodbye = say_goodbye_fn };

i kind understand happening there, stated before have never seen before.

those things double brackets in first snippet. equal sign, dots , function name w/o brackets in sec snippet.

if post articles or titles of can online it, appreciate it.

the first 2 function pointers (how function pointers work) , then, author initializes these 2 pointers via designated initializers (check this answer).

c

No comments:

Post a Comment