Re: Kingdom function

From: Jon Nielsen (nikolai@MONTANA.COM)
Date: 08/06/98


TDRD wrote:

> char *king(struct char_data * chh, char *cp, int p)
> {
> char *kingdo;
> kingdo = malloc(50);
Why not just have 'char kingdo[50]' if you're going to set an arbitrary
size?

>  bzero(kingdo, strlen(kingdo));
Not sure what bzero does.

>  char *kingdo;
>
> sprintf(kingdo, "%s",king(ch,"no Kingdom",TRUE));
> sprintf(buf, "%sYou are a member of %s.\r\n",buf,kingdo);
You're using an uninitialised pointer. You need to either set a certain
amount that 'kingdo' can be (e.g. char kingdo[50]), or malloc it. Don't
you think it would be much more efficient to have an array of string with
the kingdoms, and define each kingdom as a number that matches the slot
in the array and reference it by the define? i.e.

#define KINGDOM_UNDEFINED       -1
#define KINGDOM_WHATEVER        0
#define KINGDOM_SOMETHING       1
...
#define MAX_KINGDOMS            // Whatever number is greater than the last
                                // kingdom number.

char *kingdoms[MAX_KINGDOMS] = {
"Something",
"Whatever",
...
"Last kingdom"
};

sprintf(kingdo, "You are a member of the %s kingdom",
kingdoms[GET_KINGDOM(ch)]);

-- end mailer code --

---
Jon Nielsen     <nikolai@montana.com>
"To acquire knowledge, one must study; but to
aquire wisdom, one must observe."
 -Marilyn vos Savant


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



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