When I run code, function pointer doesn't give expected output
I am expecting output it should print 20Code:#include <stdio.h> void foo ( void ) { int a; a = 20; printf(" a = %d\n", a); } int main() { void(*fp)(void) = &foo; (*fp); return 0; }
When I run code, function pointer doesn't give expected output
I am expecting output it should print 20Code:#include <stdio.h> void foo ( void ) { int a; a = 20; printf(" a = %d\n", a); } int main() { void(*fp)(void) = &foo; (*fp); return 0; }
You need to enable warnings, for example:
-Wall -Werror -Wpedantic -std=c99
main.c:17:5: error: statement with no effect [-Werror=unused-value]
17 | (*fp);
The correct way: fp(); on line 17
Also, the function name is the entry point address, so you can drop the & and * operators:
Code:... void (*fp)(void) = foo; fp(); ...