Re: [?] Difference between d->character and ch

From: Daniel A. Koepke (dkoepke@california.com)
Date: 06/28/99


Today, Del uttered meekly:

> #0  0x80b2622 in edit_setup (d=0x0, number=99) at edit.c:119

d points to memory address 0x0, thus it's NULL.

> What is the difference between using , ch or , d-character..

d->character references the character through their descriptor_data
structure -- ch is just a reference to the character.  There's no
implicit guarantee, in any given code, that "ch->desc == d" and
"d->character == ch", so they can't be used interchangably.

> and when do you know that you can use one or the other?

The best answer (only answer), is when you have 'ch' and that refers to
you want it to, use it.  If you have 'd' and that refers to the
descriptor of the character you want it to, use 'd->character'.

There's no guarantee any given descriptor has an associated character
(e.g., they might not be connected to the game, yet), and there's no
guarantee that any given character has an associated descriptor (they
might be linkdead).

In a command (ACMD type function) the person who issued the command is
guaranteed to be passed as "ch" and what they typed after the command is
guaranteed to be in "argument" (note that it may very well have leading
spaces).

> ACMD (do_edit)
> {
>  struct descriptor_data *d;

ACMD is a macro that expands its first argument (in this case, do_edit)
into,

  void do_edit(struct char_data *ch, char *argument, int cmd, int subcmd)

You are passed a character reference, so you should use 'ch' if you want
to refer to that particular character.  Otherwise, you need to set d equal
to some descriptor_data object in order to use it.  Note that if you were
to set d = ch->desc, then d->character == ch, so you can omit "d" and just
use "ch".

> edit_setup(d, number);
> STATE(d) = CON_EDIT;

Since this function takes a descriptor and you presumambly want to do the
edit_setup() and STATE(d) on the person who issued the command ("ch"), you
want to use "ch->desc".  However, if edit_setup() doesn't need the
descriptor_data object, change edit_setup() to accept a char_data pointer
object (struct char_data * ch).

Your problems are due to a two problems with the understanding of
fundamentals.  Both can be classified under the general heading of
"variable use" -- one being the concept of scope, the other being the more
general concept of what variables do.  There is no implicit "right"
variable to use: only the variable which contains the information you want
it to.

-dak


     +------------------------------------------------------------+
     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     |  http://qsilver.queensu.ca/~fletchra/Circle/list-faq.html  |
     +------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/15/00 PST