Bifalgorithm post · Computer science one · Generalized Rounding
How can function pointers point to and call different functions?
How can function pointers point to and call different functions? (Computer science one · Generalized Rounding) — "double x = ptrToSqrt(2.0);\n\nSome more examp…
Ideas in this flowchart
- C function pointers allow functions to be referenced and called through pointer variables.
- A function pointer’s type specifies the return type and parameter types of the referenced function.
- int (*ptrToFunc)(int, double, char) = NULL;
- The pointer can reference any function that takes an int, double, and char and returns an int.
- A pointer to a function taking one double argument can be declared as double (*ptr)(double) = NULL;
- Assigning ptr = sqrt allows x = ptr(2.0), producing approximately 1.4142.
- Assigning ptr = fabs allows x = ptr(-10.5), producing 10.5.
- Function pointers are generally used to pass and return functions as arguments to other functions, such as callbacks.
- The callback use of function pointers is discussed further in Chapter 25.
- A function pointer can also be used in an expression such as double x = ptrToSqrt(2.0);