File: ptr.txt

package info (click to toggle)
libdumb 1:0.9.3-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, sid, stretch
  • size: 1,056 kB
  • ctags: 1,270
  • sloc: ansic: 9,403; makefile: 290; sh: 23
file content (127 lines) | stat: -rw-r--r-- 5,022 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*  _______         ____    __         ___    ___
 * \    _  \       \    /  \  /       \   \  /   /       '   '  '
 *  |  | \  \       |  |    ||         |   \/   |         .      .
 *  |  |  |  |      |  |    ||         ||\  /|  |
 *  |  |  |  |      |  |    ||         || \/ |  |         '  '  '
 *  |  |  |  |      |  |    ||         ||    |  |         .      .
 *  |  |_/  /        \  \__//          ||    |  |
 * /_______/ynamic    \____/niversal  /__\  /____\usic   /|  .  . ibliotheque
 *                                                      /  \
 *                                                     / .  \
 * ptr.txt - Pointer explanation.                     / / \  \
 *                                                   | <  /   \_
 *                                                   |  \/ /\   /
 *                                                    \_  /  > /
 *                                                      | \ / /
 *                                                      |  ' /
 *                                                       \__/
 */


A pointer is a small variable (often the same size as an int BUT NOT ALWAYS)
that holds the address of something in memory. You create a pointer by adding
a * to a variable, as follows:

   int x, *y;

   x = 5;
   y = &x;

The & means 'address of', so &x gives us a pointer to x. We are storing it in
y.

   (*y)++;

The * here means 'value at'. It's known as the 'dereferencing' operator. When
written before a pointer, as it is here, it allows you to treat the value
like a normal variable. In this case we are incrementing the value. If we
look at x, we'll find that it now contains 6, not 5.

   y++;

Here we are incrementing the pointer itself. This is useful for traversing
through an array, but in this particular example it is not much use.

   *y++;

Beware; this will increment the pointer, not the value stored there. It will
return the value stored at the pointer (before incrementing the pointer), so
you can use this in a bigger expression. This is why we needed brackets in
the first example.

Note that you will not need these three examples when working with DUMB; they
are simply to help illustrate the idea of pointers.

Also be aware that when defining pointers you attach the * to the variable,
not to the type. The following example will create a pointer and an int, not
two pointers:

   int *a, b;

That is why I believe it's a good idea to put a space before the * and not
after it, although programmers are divided on this.

   y = 0;
   y = NULL;

These two statements are equivalent. 0, or NULL, is a special value that is
guaranteed to have a different value from any valid pointer. This is most
often used to indicate that something doesn't point anywhere. DUMB's
functions may return it on occasion. However, in simple usage of DUMB, you
will not actually need to check for it.

Some of DUMB's functions return pointers to structs. (A struct is an
aggregration of other variables, such as ints, pointers, or other structs.
You can generally treat a struct as a single unit.) Here's an example of such
a function:

   DUH *dumb_load_it(const char *filename);

You do not know what the DUH struct actually contains; dumb.h and aldumb.h
only give the compiler enough information to deal with pointers to them. DUMB
will take charge of everything that happens inside a DUH struct.

The above function will create a DUH struct for you. First it allocates
the memory it needs, then it fills the struct with data, then it returns a
pointer. This DUH struct will contain the data necessary to play an IT file.
You can define a suitable variable and store the pointer in it as follows:

   DUH *duh = dumb_load_it("music.it");

Or this can be split up:

   DUH *duh;
   duh = dumb_load_it("music.it");

In order to use this DUH struct later, you must pass its pointer to other
functions. To pass the pointer to a function, simply write 'duh' for the
appropriate parameter.

When you've finished with a DUH struct (this applies equally to the other
structs DUMB deals with), you must pass it to an appropriate function for
freeing up the memory:

   unload_duh(duh);

After you've done this, the memory will no longer be allocated, and the
pointer will have no meaning. You may wish to set it to NULL at this point
for safety. Alternatively just be sure not to use the present value of the
pointer any more. You can of course assign a new value to the pointer, e.g.
by calling dumb_load_it() again.

Note the following:

   DUH *duh2 = duh;

This only duplicates the pointer, not the DUH itself. You still only have one
copy of the DUH. There is no way of duplicating a DUH, short of loading it
twice. This is not a problem, because DUMB can play it 'twice at the same
time' anyway.

That should be all you need to know about pointers in order to use DUMB. If
there's anything you feel should be explained better here, or anything else
that should be added, please don't hesitate to let me know!


Ben Davis
entheh@users.sf.net