mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-08-13 18:24:39 +02:00
#102, moving function pointers section to the end of the doc
This commit is contained in:
@@ -323,33 +323,6 @@ str_reverse(c);
|
|||||||
printf("%s\n", c); // => ".tset a si sihT"
|
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
|
// User-defined types and structs
|
||||||
///////////////////////////////////////
|
///////////////////////////////////////
|
||||||
@@ -390,6 +363,36 @@ int area(rect r){
|
|||||||
return r.width * r.height;
|
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
|
## Further Reading
|
||||||
|
Reference in New Issue
Block a user