From 3308c9c015e130b4e813f998898852d9fc2d7e05 Mon Sep 17 00:00:00 2001 From: sergiokas Date: Wed, 3 Jul 2013 23:57:37 -0300 Subject: Adding small function pointers example --- c.html.markdown | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/c.html.markdown b/c.html.markdown index 69bf099e..fba587fc 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -323,6 +323,30 @@ str_reverse(c); printf("%s\n", c); // => ".tset a si sihT" */ +/* +Functions are located in known memory addresses, so they can be called +through function pointers. Syntax may be initially confusing. + +Example: use str_reverse from a pointer +*/ +void str_reverse_through_pointer(char * str_in) { + // Define a function pointer variable, named f. + void (*f)(char *); // Signature should exactly match the target function. + f = &str_reverse; // Assign the address for the actual function (determined at runtime) + (*f)(str_in); // Just calling the function through the pointer + // f(str_in); // That's an alternate but equally valid syntax for calling it. +} + +/* +As long as function signatures match, you can assign any function to the same pointer. +Useful for passing handlers (or callback functions) around. +Function pointers are usually typedef'd for simplicity and readability, as follows: + +typedef void (*my_fnp_type)(char *); +... +my_fnp_type f; +*/ + /////////////////////////////////////// // User-defined types and structs /////////////////////////////////////// -- cgit v1.2.3 From 0376e0807a796b9c38285e908589b8ef3f2ded21 Mon Sep 17 00:00:00 2001 From: sergiokas Date: Thu, 4 Jul 2013 00:00:49 -0300 Subject: Update c.html.markdown --- c.html.markdown | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/c.html.markdown b/c.html.markdown index fba587fc..23d97560 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -341,11 +341,14 @@ void str_reverse_through_pointer(char * str_in) { As long as function signatures match, you can assign any function to the same pointer. Useful for passing handlers (or callback functions) around. Function pointers are usually typedef'd for simplicity and readability, as follows: +*/ typedef void (*my_fnp_type)(char *); -... -my_fnp_type f; -*/ + +// The used when declaring the actual pointer variable: +// ... +// my_fnp_type f; + /////////////////////////////////////// // User-defined types and structs -- cgit v1.2.3 From fff3f61e2c64745eb4404ccaf582730e218043cf Mon Sep 17 00:00:00 2001 From: sergiokas Date: Thu, 4 Jul 2013 11:42:36 -0300 Subject: #102, moving function pointers section to the end of the doc --- c.html.markdown | 57 ++++++++++++++++++++++++++++++--------------------------- 1 file changed, 30 insertions(+), 27 deletions(-) diff --git a/c.html.markdown b/c.html.markdown index 23d97560..f5f28608 100644 --- a/c.html.markdown +++ b/c.html.markdown @@ -323,33 +323,6 @@ str_reverse(c); printf("%s\n", c); // => ".tset a si sihT" */ -/* -Functions are located in known memory addresses, so they can be called -through function pointers. Syntax may be initially confusing. - -Example: use str_reverse from a pointer -*/ -void str_reverse_through_pointer(char * str_in) { - // Define a function pointer variable, named f. - void (*f)(char *); // Signature should exactly match the target function. - f = &str_reverse; // Assign the address for the actual function (determined at runtime) - (*f)(str_in); // Just calling the function through the pointer - // f(str_in); // That's an alternate but equally valid syntax for calling it. -} - -/* -As long as function signatures match, you can assign any function to the same pointer. -Useful for passing handlers (or callback functions) around. -Function pointers are usually typedef'd for simplicity and readability, as follows: -*/ - -typedef void (*my_fnp_type)(char *); - -// The used when declaring the actual pointer variable: -// ... -// my_fnp_type f; - - /////////////////////////////////////// // User-defined types and structs /////////////////////////////////////// @@ -390,6 +363,36 @@ int area(rect r){ return r.width * r.height; } +/////////////////////////////////////// +// Function pointers +/////////////////////////////////////// +/* +At runtime, functions are located at known memory addresses. Function pointers are +much likely any other pointer (they just store a memory address), but can be used +to invoke functions directly, and to pass handlers (or callback functions) around. +However, definition syntax may be initially confusing. + +Example: use str_reverse from a pointer +*/ +void str_reverse_through_pointer(char * str_in) { + // Define a function pointer variable, named f. + void (*f)(char *); // Signature should exactly match the target function. + f = &str_reverse; // Assign the address for the actual function (determined at runtime) + (*f)(str_in); // Just calling the function through the pointer + // f(str_in); // That's an alternative but equally valid syntax for calling it. +} + +/* +As long as function signatures match, you can assign any function to the same pointer. +Function pointers are usually typedef'd for simplicity and readability, as follows: +*/ + +typedef void (*my_fnp_type)(char *); + +// The used when declaring the actual pointer variable: +// ... +// my_fnp_type f; + ``` ## Further Reading -- cgit v1.2.3