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!
=== C and C++ === <syntaxhighlight lang="C"> // This declares 'F', a function that accepts a 'char' and returns an 'int'. Definition is elsewhere. int F(char c); // This defines 'Fn', a type of function that accepts a 'char' and returns an 'int'. typedef int Fn(char c); // This defines 'fn', a variable of type pointer-to-'Fn', and assigns the address of 'F' to it. Fn *fn = &F; // Note '&' not required - but it highlights what is being done. // This calls 'F' using 'fn', assigning the result to the variable 'a' int a = fn('A'); // This defines 'Call', a function that accepts a pointer-to-'Fn', calls it, and returns the result int Call(Fn *fn, char c) { return fn(c); } // Call(fn, c) // This calls function 'Call', passing in 'F' and assigning the result to 'call' int call = Call(&F, 'A'); // Again, '&' is not required // LEGACY: Note that to maintain existing code bases, the above definition style can still be used first; // then the original type can be defined in terms of it using the new style. // This defines 'PFn', a type of pointer-to-type-Fn. typedef Fn *PFn; // 'PFn' can be used wherever 'Fn *' can PFn pfn = F; int CallP(PFn fn, char c); </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)