Top Mud Sites Forum

Top Mud Sites Forum (http://www.topmudsites.com/forums/index.php)
-   MUD Coding (http://www.topmudsites.com/forums/forumdisplay.php?f=9)
-   -   Help out a newbie? (http://www.topmudsites.com/forums/showthread.php?t=298)

Sitral 04-24-2002 06:05 PM

Let me start by saying I'm as green as they get, so bear with me.  I'm also not familiar with this type of forum, so I hope this posts correctly...

This _seems_ simple enough, but I'm running into errors.  The long and short of it is this: I want to take what a character puts in and store it in a structure, along these lines:

[code]
struct form { char * input[MAX_STRING_LENGTH]; };
struct form structure { '\0' };
for( i=0; structire.input[i] != '\0'; i++ ) {}
/* If there's something there, skip it! */
structure.input[i] = argument;
/* After we found a blank spot, store the input. */
[/quote]

I want the user to be able to "store say haha!", "store say You're under my power!", "store say Foolish mortal!", then be able to execute all three says with one command. It stores the first one fine, but when I try to store a second command, it changes the first to what I want the second to be, and adds that second.  When I store a third command, all three become that last command.  The above example would store "say Foolish mortal!" three times.

First thing that came to my mind is that there had to be a problem with my for statement, but a bit of elementary debugging seems to prove it works fine.  I then recalled (and verified) that every instance of an array in my book (a poor one, I'm told) only has single characters.  Can 'structure.input[0] = say hahaha!'?  Can/Must I use a multi-dimentional array to store strings?  That last didn't seem to work for me...

Hope it was clear and concise. Thanks.

Yui Unifex 04-24-2002 06:53 PM

Yes you must have a multidimensional array to handle this sort of thing. But I'm guessing that you're not properly handling this array, and that's what's causing your problems. Here's an implementation that should work. A note of warning though, it's untested =)

[code] /* on the character structure */
char **storage;
int storage_size;

/* on character initialization */
character->storage_size = 0;

/* in your storage command */
int i;
char **new_storage;

++character->storage_size;
new_storage = malloc(sizeof(char **) * character->storage_size);

for (i = 0; i < character->storage_size - 1; ++i)
new_storage[i] = character->storage[i];
free(character->storage);

new_storage[character->storage_size - 1] = strdup(new_thing_to_store);
character->storage = new_storage;

/* on character deletion */
int i;
for (i = 0; i < character->storage_size; ++i)
free(character->storage[i]);
free(character->storage);[/quote]

It would be wisest to make those into seperate functions that operate on the storage array and size, but that should give you an idea of how to do it.

Sitral 04-24-2002 08:15 PM


Koryon 04-24-2002 10:56 PM


Yui Unifex 04-25-2002 06:23 AM

No he doesn't =P. str_dup() is a Diku function. strdup() is a BSD function, and I free it with free().

Seth 04-25-2002 06:30 AM

On the other hand, you could tell players to get a decent client with scripting functions, then give them a good VB/J/Javascript site with tutorials.

Koryon 04-25-2002 07:23 AM


Sitral 04-26-2002 07:42 AM

Heh, won't quite fit what I'm working towards, Seth.  Previous examples were used to (hopefully) keep things simple.

Thanks again, Yui.  And thanks, Koryon.  Doesn't completely make sense, but when I need to learn it, it'll likely make it a bit easier.

GenmaC 05-02-2002 02:34 PM

You could handle this with a double/single linked list too, but that's just the crack in my veins talking.


All times are GMT -4. The time now is 06:19 AM.

Powered by vBulletin® Version 3.6.7
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright Top Mud Sites.com 2022