C FunctionsLesson 31Free preview
C Function Pointers
This lesson is part of C · All levels
Summary
A pointer is a variable that stores the address of a function, so that the function can be stored in the variable, sent as an argument, or called indirectly. The declaration is quite complicated because it involves a * sign, for example int (*op)(int, int), and is called as a normal function. This pattern became the basis of callback techniques and dispatch tables, for example choosing arithmetic operations based on user preferences. Understanding the pointer paves the way to more flexible program design.
Key points
- The pointer keeps the address of the function and is called through the variable
- The type of return and the parameters of the function designated shall be the same as the pointer declaration
- Useful for callbacks, strategies, and operating tables that you can choose during runtime
- Often used in qsort with a special comparator of stdlib.h
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.