What is function pointer and need of function pointer | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What is function pointer and need of function pointer

28th Jun 2019, 7:16 AM
Nani
Nani - avatar
1 Answer
+ 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
28th Jun 2019, 7:50 AM
Agent_I
Agent_I - avatar