However, when I continue to try different combinations instead of analyzing the results, I am now just lost in the process.
I hope you Can help me figure out this mess.
First code example
typedef void (print)(void);
void do_something ( void) {printf("Hello World\n"); }
print *pr;
pr = &do_something;
pr(); // Hello World
Second code example
typedef void (print)(void);
void do_something (void) {printf("Hello World\n") ; }
print *pr;
pr = do_something;
pr(); // Hello World
How does the code example above work, as if ‘&’ has no effect on function names
The third code example
typedef void (print)(void);
void do_something ( void) {printf("Hello World\n"); }
print pr;
pr = do_something; // compile error
pr = &do_something; // compile error< br />pr();
I hope one of the tasks above will work here, but damn it! I really don’t understand function pointers (maybe typedef).
Similarly, when using function pointers, multiple dereferences are not a problem:
#include
typedef void print(void);
static void dosomething(void) {printf("Hello World\n"); }
int main(void)
{
print *f1 = dosomething;
print *f2 = &dosomething;
f2();
(f1)();
(*f1)();
(**f2)();
(***f1)();
(****f2)();
(*****f1)();
}
Compile fully under the following conditions:
gcc- O3 -g -Wall -Wextra -Werror -Wmissing-prototypes -Wstrict-prototypes \
-Wold-style-definition -std=c99 xx.c -o xx
I won’t say more Stars are good style; it is not. It is’strange, (yes, you might say) abnormal’. One is enough (one star is mainly for people like me who learned to program in C before the standard Say “You can call a function by a pointer without using the (* pointer_to_function) (arg1, arg2) notation; if you like “), you can write pointer_to_function(arg1, arg2). Yes, this is strange. No, nothing else Types (or categories) exhibit the same behavior, thank goodness.
I think I may suffer from the terrible “accidental programmer” disease, at least when it comes to typedefs and function pointers Time. So I have been experimenting with various combinations involving these, according to what I got
However, when I continue to try different combinations instead of analyzing the results, I am now just lost in the process.
I hope you Can help me figure out this mess.
First code example
typedef void (print)(void);
void do_something ( void) {printf("Hello World\n"); }
print *pr;
pr = &do_something;
pr(); // Hello World
Second code example
typedef void (print)(void);
void do_something (void) {printf("Hello World\n") ; }
print *pr;
pr = do_something;
pr(); // Hello World
How does the code example above work, as if ‘&’ has no effect on function names
The third code example
typedef void (print)(void);
void do_something ( void) {printf("Hello World\n"); }
print pr;
pr = do_something; // compile error
pr = &do_something; // compile error< br />pr();
I hope one of the tasks above will work here, but damn it! I really don’t understand function pointers (maybe typedef).
The address of the function name and the ordinary function name are the same, so & is for the function name No effect.
Similarly, when using function pointers, multiple dereferences are not a problem:
#include< br />typedef void print(void);
static void dosomething(void) {printf("Hello World\n"); }
int main(void)
{
print *f1 = dosomething;
print *f2 = &dosomething;
f2();
(f1)();
(*f1)();< br /> (**f2)();
(***f1)();
(****f2)();
(*****f1) ();
}
Compile fully under the following conditions:
gcc -O3 -g -Wall -Wextra -Werror -Wmissing- prototypes -Wstrict-prototypes \
-Wold-style-definition -std=c99 xx.c -o xx
I will not say that multiple stars are good style; it is not. It is ‘Weird, (yes, you might say) abnormal’. One is enough (a star is mainly for people like me, who learned to use C programming before the standard said “You can call a function through a pointer without using (* pointer_to_function) (arg1, arg2) notation; if you like “), you can write pointer_to_function(arg1, arg2). Yes, this is strange. No, no other types (or categories) exhibit the same behavior, thank God .