Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Function pointer
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Simple function pointers== The simplest implementation of a function (or subroutine) pointer is as a [[variable (computer science)|variable]] containing the [[memory address|address]] of the function within executable memory. Older [[third-generation programming language|third-generation languages]] such as [[PL/I]] and [[COBOL]], as well as more modern languages such as [[Pascal (programming language)|Pascal]] and [[C (programming language)|C]] generally implement function pointers in this manner.<ref>{{cite web | access-date = 2011-04-13 | publisher = logo | title = The Function Pointer Tutorials | quote = Important note: A function pointer always points to a function with a specific signature! Thus all functions, you want to use with the same function pointer, must have the same parameters and return-type! | url = http://www.newty.de/fpt/intro.html#top | archivedate = 2011-05-16 | archiveurl = https://web.archive.org/web/20110516153643/http://www.newty.de/fpt/intro.html#top | url-status = deviated }}</ref> === Example in C === {{See also|#Alternate C and C++ syntax}} The following C program illustrates the use of two function pointers: * ''func1'' takes one double-precision (double) parameter and returns another double, and is assigned to a function which converts centimeters to inches. * ''func2'' takes a pointer to a constant character array as well as an integer and returns a pointer to a character, and is assigned to a [[C string handling]] function which returns a pointer to the first occurrence of a given character in a character array. <syntaxhighlight lang="c"> #include <stdio.h> /* for printf */ #include <string.h> /* for strchr */ double cm_to_inches(double cm) { return cm / 2.54; } // "strchr" is part of the C string handling (i.e., no need for declaration) // See https://en.wikipedia.org/wiki/C_string_handling#Functions int main(void) { double (*func1)(double) = cm_to_inches; char * (*func2)(const char *, int) = strchr; printf("%f %s", func1(15.0), func2("Wikipedia", 'p')); /* prints "5.905512 pedia" */ return 0; } </syntaxhighlight> The next program uses a function pointer to invoke one of two functions (<code>sin</code> or <code>cos</code>) indirectly from another function (<code>compute_sum</code>, computing an approximation of the function's [[Riemann integration]]). The program operates by having function <code>main</code> call function <code>compute_sum</code> twice, passing it a pointer to the library function <code>sin</code> the first time, and a pointer to function <code>cos</code> the second time. Function <code>compute_sum</code> in turn invokes one of the two functions indirectly by dereferencing its function pointer argument <code>funcp</code> multiple times, adding together the values that the invoked function returns and returning the resulting sum. The two sums are written to the standard output by <code>main</code>. <syntaxhighlight lang="c" line="1"> #include <math.h> #include <stdio.h> // Function taking a function pointer as an argument double compute_sum(double (*funcp)(double), double lo, double hi) { double sum = 0.0; // Add values returned by the pointed-to function '*funcp' int i; for (i = 0; i <= 100; i++) { // Use the function pointer 'funcp' to invoke the function double x = i / 100.0 * (hi - lo) + lo; double y = funcp(x); sum += y; } return sum / 101.0 * (hi - lo); } double square(double x) { return x * x; } int main(void) { double sum; // Use standard library function 'sin()' as the pointed-to function sum = compute_sum(sin, 0.0, 1.0); printf("sum(sin): %g\n", sum); // Use standard library function 'cos()' as the pointed-to function sum = compute_sum(cos, 0.0, 1.0); printf("sum(cos): %g\n", sum); // Use user-defined function 'square()' as the pointed-to function sum = compute_sum(square, 0.0, 1.0); printf("sum(square): %g\n", sum); return 0; } </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)