Because of the intended
audience, I’m going to take a moment and attempt to explain pointers. Pointers are wonderful, I love them, I hate
java because it doesn’t have pointers (among other reasons). They also can be confusing if you’ve never
used them, or if you come from a background of Qbasic or Visual Basic type
programming. This is basically how
pointers work.
You define an integer by
using int x; now say you have a function that you want it to have some
parameters that you want to modify their values. Pointers work perfectly in this case (unless your using C++ and
can pass by value but I’m assuming not).
So say you want a function that tells the level range of a pkiller. So you create the function
void get_range(CHAR_DATA*ch, int *low, int *high)
*low = ch->level - 5;
*high = ch->level + 5;
if(*low < 5) *low = 5;
if(*high > LEVEL_AVATAR) *high = LEVEL_AVATAR;
return;
}
Now all you have to do is,
pass the address of two integers and the CHAR_DATA, which is already refered to
by a pointer, and those intengers will then have the values aassigned to them
in the get_range function. Example:
int lo, hi;
get_range(ch, &lo,
&hi)l
Now lo has the low level,
and hi has the high level. But you may
be asking what do all those things represent.
Here’s what they do in a nutshell.
int *x; defines a pointer, x, to a type integer. So the parameters in the function get_range
are expecting two pointers to integers.
The way to access the address of a non pointer, is to use the &
operand. So if you have int x, to
access it’s pointer you do &x, like I did in the example above.
Strings in C are also
pointers, for instance in the functions that you used ch_printf, and then a
string, it’s really just a pointer to a char type. String must end with a ‘\0’ or null bit (The end of a literal
string like “test” automatically have a terminating null bit).
Arrays are also a series of
pointers. Basically say you have int
array[10]; *array is the same as
array[0]; and you can do pointer arithmetic, which I won’t go into detail
about. Anyhow I hope that clarifies
some things, here’s some code to think about, if you understand it, you
understand pointers J
char buf[100], buf2[100]; char *ptr1 = buf, *ptr2 =
buf2;
while(*(ptr1++)=*(ptr2++));
What this does is copy the
contents of buf2 into buf. I’d strongly
recremmend learning pointers from a real source though, like a book or website
tutorial or something, because they can be handy, useful and they are used all
throughout the code, though you could get along without understanding them, it
would help. From here on in, it is
assumed that you understand pointers.