summaryrefslogtreecommitdiff
path: root/apps/codecs/dumb/docs/fnptr.txt
diff options
context:
space:
mode:
Diffstat (limited to 'apps/codecs/dumb/docs/fnptr.txt')
-rw-r--r--apps/codecs/dumb/docs/fnptr.txt113
1 files changed, 0 insertions, 113 deletions
diff --git a/apps/codecs/dumb/docs/fnptr.txt b/apps/codecs/dumb/docs/fnptr.txt
deleted file mode 100644
index a5fb216822..0000000000
--- a/apps/codecs/dumb/docs/fnptr.txt
+++ /dev/null
@@ -1,113 +0,0 @@
1/* _______ ____ __ ___ ___
2 * \ _ \ \ / \ / \ \ / / ' ' '
3 * | | \ \ | | || | \/ | . .
4 * | | | | | | || ||\ /| |
5 * | | | | | | || || \/ | | ' ' '
6 * | | | | | | || || | | . .
7 * | |_/ / \ \__// || | |
8 * /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
9 * / \
10 * / . \
11 * fnptr.txt - Function pointer explanation. / / \ \
12 * | < / \_
13 * | \/ /\ /
14 * \_ / > /
15 * | \ / /
16 * | ' /
17 * \__/
18 */
19
20
21C allows you to create and use function pointers. A function pointer is a
22variable that points to a function, and you can use it to call that function.
23Why is this useful?
24
25Function pointers can be passed as parameters. As an example, here's a
26function from Allegro:
27
28 void create_light_table(COLOR_MAP *table, const PALETTE pal, int r, g, b,
29 void (*callback)(int pos));
30
31Don't worry about the syntax just yet, but the last parameter, 'callback', is
32a pointer to a function that takes an int parameter. create_light_table() can
33take some time to complete its work, and you may want to display a progress
34indicator. So you write a function to draw the progress indicator, and then,
35for 'callback', you specify a pointer to your function. This will enable
36create_light_table() to call your function at intervals during its
37processing. (If you don't want to use the callback, you can pass NULL, but
38this only works because create_light_table() checks actively for NULL. You
39can't always specify NULL when you want nothing to happen.)
40
41There are many other uses. In addition to using function pointers as
42parameters, Allegro has some global function pointers you can set to point to
43your functions. Function pointers can also be used in structs, and this is
44where DUMB makes the most use of them.
45
46So how are they used?
47
48 void bar(void) { ... } /* Here's a function */
49 void (*foo)(void) = &bar; /* Take a pointer */
50 (*foo)(); /* Call the function */
51
52 char *baz(float a) { ... } /* Here's another function */
53 char *(*foobarbaz)(float a) = &baz; /* Take a pointer */
54 char *rv = (*foobarbaz)(0.1); /* Call the function */
55
56In both these cases, note how the statement for calling the pointed-to
57function (third line) resembles the definition of the function pointer
58(second line). This is true of any variable in C, and can lead to some truly
59obfuscated definitions if you are that way inclined. Such definitions can be
60clarified with typedefs, but before you use those, it is important you
61understand how the above statements work. I speak from experience: function
62pointer notation looks random and scary, until you understand why it's the
63way it is; then it makes perfect sense.
64
65(It is actually permissible to omit the & when taking a pointer and to write
66e.g. foobarbaz(0.1) instead of (*foobarbaz)(0.1). However, I recommend not
67doing this, since the syntax for using the pointer no longer resembles the
68definition. Writing e.g. (*foobarbaz)(0.1) also makes a clear distinction
69between function pointer calls and ordinary function calls, which makes code
70more readable.)
71
72Note that function pointers have the return value and parameter list
73specified. A function pointer can only point to a function with a matching
74return value and matching parameters. (You can break this rule by casting the
75pointer explicitly, but there is no situation where doing so is portable to
76all computers, and I strongly advise against it unless you're writing system
77code. If you're not sure whether you're writing system code or not, then
78you're not.)
79
80The parameter names need not match (although the types must). If you wish to
81rename a parameter in your function, you do not have to change the function
82pointer accordingly. In fact, when you define a function pointer, you don't
83even have to specify the names of parameters if you don't want to. I normally
84do so for clarity.
85
86It is possible to typedef a function pointer. In order to typedef a function
87pointer, you start by declaring the pointer as a variable:
88
89 void (*myfunc)(void);
90
91Then you write 'typedef' before it and replace the variable name, which is
92myfunc, with the type name (this rule can be applied to any variable when you
93want to use typedef):
94
95 typedef void (*MYTYPE)(void);
96
97Now 'MYTYPE' represents a pointer to a function with no parameters and no
98return value. The following two lines are completely equivalent:
99
100 MYTYPE myfunc;
101 void (*myfunc)(void);
102
103Note that we use MYTYPE without an asterisk (*), since it is already a
104pointer.
105
106That's it. If you feel anything should be explained better here, or if you
107feel something should be added, please don't hesitate to let me know!
108
109
110Ben Davis
111entheh@users.sf.net
112IRC EFnet #dumb
113See readme.txt for details on using IRC.