summaryrefslogtreecommitdiffhomepage
path: root/c.html.markdown
diff options
context:
space:
mode:
Diffstat (limited to 'c.html.markdown')
-rw-r--r--c.html.markdown38
1 files changed, 35 insertions, 3 deletions
diff --git a/c.html.markdown b/c.html.markdown
index 69bf099e..d243b19d 100644
--- a/c.html.markdown
+++ b/c.html.markdown
@@ -1,8 +1,10 @@
---
+name: c
+category: language
language: c
-author: Adam Bard
-author_url: http://adambard.com/
filename: learnc.c
+contributors:
+ - ["Adam Bard", "http://adambard.com/"]
---
Ah, C. Still the language of modern high-performance computing.
@@ -68,7 +70,7 @@ double x_double = 0.0;
// Integral types may be unsigned. This means they can't be negative, but
// the maximum value of an unsigned variable is greater than the maximum
-// value of the same size.
+// signed value of the same size.
unsigned char ux_char;
unsigned short ux_short;
unsigned int ux_int;
@@ -363,6 +365,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