I think I may be suffering from the dreaded "accidental programmer" disease, at least when it comes to typedefs and function pointers. So I've been experimenting with all kinds of combinations involving these to analyse the results based on all the output I get.
But as I kept on trying different combinations, instead of analyzing the results I'm now just lost in process.
I'm hoping you guys will help me figure out this mess.
First code example
typedef void (print)(void);
void do_something (void) { printf("Hello World
"); }
print *pr;
pr = &do_something;
pr(); // Hello World
Second code example
typedef void (print)(void);
void do_something (void) { printf("Hello World
"); }
print *pr;
pr = do_something;
pr(); // Hello World
How do both the above code examples work, it's as if '&' has no effect on function name
third code example
typedef void (print)(void);
void do_something (void) { printf("Hello World
"); }
print pr;
pr = do_something; // compile error
pr = &do_something; // compile error
pr();
I was hoping one of the above assignments to work here but damn! I really don't understand function pointers (and maybe typedef too).
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…