We just learnt in school about function pointers in C and i wanted to try them out in a test program. The idea is obviously pretty simple. However, when i try to use result = (*funcPtr)(); i get an STATUS_ACCESS_VIOLATION exception but i can't tell what i did wrong. Any ideas on what i'm missing?
#include <stdio.h>
int fun1();
int fun2();
int (*funcPtr)(void);
int fun1() {
return 1;
}
int fun2() {
return 2;
}
int main(void) {
int input,result = 0;
scanf("%d",input);
if(input == 1) {
funcPtr = &fun1;
} else if(input == 2) {
funcPtr = &fun2;
}
result = (*funcPtr)();
printf("%d\n",result);
}
scanf(scanf("%d",input);) not with function pointer. You are missing&.inputis neither1nor2.scanf()(void)in place of()in the function declarations and definitions. There is a difference.