Concept

What is a function pointer and how is it declared?

ComputerScienceOne / Function Pointers

"Now that we have comparator functions to order elements, we need a way of passing a comparator to a generic search or sort function so that it can be used by that function. To achieve this in C, we use function pointers. Recall that a pointer is simply a reference to some memory location. As we’ve already seen, pointers can point to variables of simple data types such as int or double, arrays of these types or even user-defined structures. We’ve also seen that pointers can be generic using the void keyword. The malloc() function for example, returned a generic void pointer, void *, to allow it to be used to allocate memory for any type. Generic void pointers simply point to the start of a memory block, not necessarily to a memory location of any particular type of variable. As we’ve also already seen, the program code itself lives in memory (the stack). It thus makes sense that we could reference a memory location that, instead of containing variables, contains executable code, in particular a function. This is what a function pointer does: it points to a memory location where the code for the function is stored.\n\nTo declare a function pointer, we need to specify more information than a typical int * or double * pointer. Since it points to a function, we need to specify the function’s signature: its return type and parameter list. Consider the following example:\n\nint (*ptrToFunc)(int, double, char) = NULL;\n\nIn this declaration, we’ve created a function pointer named ptrToFunc. This pointer is capable of pointing to any function whose return type is int (indicated by the first keyword) and which takes three parameters: an int, a double and a char. The assignment operation has initialized this pointer to NULL.\n\nConsider another example: let’s declare a function pointer that is capable of pointing to the math library’s sqrt() function. The sqrt() function returns a double and takes a single double parameter. Adapting the syntax above, we would create this pointer as follows.\n\ndouble (*ptrToSqrt)(double) = NULL;\n\nAgain, we have initialized it to NULL; it does not yet reference the sqrt() function. To make it reference this function, we need to assign it a value. Recall that to get the memory location of a regular variable, say int x;, we use the referencing operator, &x. Similarly, with functions we can use the referencing function, in this case &sqrt to get the memory address of the sqrt() function, however this is not necessary. Similar to arrays, the identifier (name) of a function serves as its memory address! The identifier sqrt itself is the memory location of the function. Thus to assign ptrToSqrt to point to sqrt(), we simply need to do the following:\n\nptrToSqrt = sqrt;\n\nNote the difference: usually we invoke sqrt() by writing parentheses and providing an argument. When referencing the function itself, we omit the parentheses."

Related Ideas

What is a function pointer and how is it declared? | ComputerScienceOne | Bifalgorithm | Bifalgorithm