summaryrefslogtreecommitdiff
path: root/apps/codecs/dumb/docs/ptr.txt
diff options
context:
space:
mode:
Diffstat (limited to 'apps/codecs/dumb/docs/ptr.txt')
-rw-r--r--apps/codecs/dumb/docs/ptr.txt129
1 files changed, 129 insertions, 0 deletions
diff --git a/apps/codecs/dumb/docs/ptr.txt b/apps/codecs/dumb/docs/ptr.txt
new file mode 100644
index 0000000000..0eb42ccf02
--- /dev/null
+++ b/apps/codecs/dumb/docs/ptr.txt
@@ -0,0 +1,129 @@
1/* _______ ____ __ ___ ___
2 * \ _ \ \ / \ / \ \ / / ' ' '
3 * | | \ \ | | || | \/ | . .
4 * | | | | | | || ||\ /| |
5 * | | | | | | || || \/ | | ' ' '
6 * | | | | | | || || | | . .
7 * | |_/ / \ \__// || | |
8 * /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
9 * / \
10 * / . \
11 * ptr.txt - Pointer explanation. / / \ \
12 * | < / \_
13 * | \/ /\ /
14 * \_ / > /
15 * | \ / /
16 * | ' /
17 * \__/
18 */
19
20
21A pointer is a small variable (often the same size as an int BUT NOT ALWAYS)
22that holds the address of something in memory. You create a pointer by adding
23a * to a variable, as follows:
24
25 int x, *y;
26
27 x = 5;
28 y = &x;
29
30The & means 'address of', so &x gives us a pointer to x. We are storing it in
31y.
32
33 (*y)++;
34
35The * here means 'value at'. It's known as the 'dereferencing' operator. When
36written before a pointer, as it is here, it allows you to treat the value
37like a normal variable. In this case we are incrementing the value. If we
38look at x, we'll find that it now contains 6, not 5.
39
40 y++;
41
42Here we are incrementing the pointer itself. This is useful for traversing
43through an array, but in this particular example it is not much use.
44
45 *y++;
46
47Beware; this will increment the pointer, not the value stored there. It will
48return the value stored at the pointer (before incrementing the pointer), so
49you can use this in a bigger expression. This is why we needed brackets in
50the first example.
51
52Note that you will not need these three examples when working with DUMB; they
53are simply to help illustrate the idea of pointers.
54
55Also be aware that when defining pointers you attach the * to the variable,
56not to the type. The following example will create a pointer and an int, not
57two pointers:
58
59 int *a, b;
60
61That is why I believe it's a good idea to put a space before the * and not
62after it, although programmers are divided on this.
63
64 y = 0;
65 y = NULL;
66
67These two statements are equivalent. 0, or NULL, is a special value that is
68guaranteed to have a different value from any valid pointer. This is most
69often used to indicate that something doesn't point anywhere. DUMB's
70functions may return it on occasion. However, in simple usage of DUMB, you
71will not actually need to check for it.
72
73Some of DUMB's functions return pointers to structs. (A struct is an
74aggregration of other variables, such as ints, pointers, or other structs.
75You can generally treat a struct as a single unit.) Here's an example of such
76a function:
77
78 DUH *dumb_load_it(const char *filename);
79
80You do not know what the DUH struct actually contains; dumb.h and aldumb.h
81only give the compiler enough information to deal with pointers to them. DUMB
82will take charge of everything that happens inside a DUH struct.
83
84The above function will create a DUH struct for you. First it allocates
85the memory it needs, then it fills the struct with data, then it returns a
86pointer. This DUH struct will contain the data necessary to play an IT file.
87You can define a suitable variable and store the pointer in it as follows:
88
89 DUH *duh = dumb_load_it("music.it");
90
91Or this can be split up:
92
93 DUH *duh;
94 duh = dumb_load_it("music.it");
95
96In order to use this DUH struct later, you must pass its pointer to other
97functions. To pass the pointer to a function, simply write 'duh' for the
98appropriate parameter.
99
100When you've finished with a DUH struct (this applies equally to the other
101structs DUMB deals with), you must pass it to an appropriate function for
102freeing up the memory:
103
104 unload_duh(duh);
105
106After you've done this, the memory will no longer be allocated, and the
107pointer will have no meaning. You may wish to set it to NULL at this point
108for safety. Alternatively just be sure not to use the present value of the
109pointer any more. You can of course assign a new value to the pointer, e.g.
110by calling dumb_load_it() again.
111
112Note the following:
113
114 DUH *duh2 = duh;
115
116This only duplicates the pointer, not the DUH itself. You still only have one
117copy of the DUH. There is no way of duplicating a DUH, short of loading it
118twice. This is not a problem, because DUMB can play it 'twice at the same
119time' anyway.
120
121That should be all you need to know about pointers in order to use DUMB. If
122there's anything you feel should be explained better here, or anything else
123that should be added, please don't hesitate to let me know!
124
125
126Ben Davis
127entheh@users.sf.net
128IRC EFnet #dumb
129See readme.txt for details on using IRC.