+ 1
What is function pointer and need of function pointer
2 ответов
+ 1
A function pointer is a pointer to a function. It can be declared like this int (*func)(int), it means that it's a pointer to a function that returns int as its return value and takes int as its argument. A function pointer can have many uses. One of them is when you need to run different function for different input, like this
void print1() {
printf("1\n");
}
void print2() {
printf("2\n");
}
void print3() {
printf("3\n");
}
int main() {
void (*print[])() = { print1, print2, print3 };
int x;
scanf("%d", &x);
print[x]();
return 0;
}
You can use array of function pointer instead of switch statement