Top Mud Sites Forum Return to TopMudSites.com
Go Back   Top Mud Sites Forum > Mud Development and Administration > MUD Coding
Click here to Register

Reply
 
Thread Tools
Old 04-24-2002, 06:05 PM   #1
Sitral
New Member
 
Join Date: Apr 2002
Posts: 11
Sitral is on a distinguished road
Red face

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.
Sitral is offline   Reply With Quote
Old 04-24-2002, 06:53 PM   #2
Yui Unifex
Senior Member
 
Join Date: Apr 2002
Location: Florida
Posts: 323
Yui Unifex is on a distinguished road
Send a message via ICQ to Yui Unifex Send a message via AIM to Yui Unifex
Question

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.
Yui Unifex is offline   Reply With Quote
Old 04-24-2002, 08:15 PM   #3
Sitral
New Member
 
Join Date: Apr 2002
Posts: 11
Sitral is on a distinguished road
Exclamation

Surprisingly, I think I understood most of what was going on in your example, aside from "char ** storage" and the like.  How does that differ from "char * storage"?

I'm trying to keep things local while I'm still a guppy in this hurricane of information, so I looked only at what was needed in that function.  Unless I'm mistaken, most of the differences are for memory management and avoidance of leaks...if that's true, I guess it means my code was theoretically correct, but practically dangerous.

Sure enough, the only thing that struck me as something not related to memory fixed my problem.  I didn't use strdup() when I copied the argument into the structure.  Wonder why such a big difference...Ah well, thought for another day I guess.

Hmm, I got distracted, can't remember what else I wanted to write.   Guess I'll just close it, then.  Thanks again for your help!  Your friendliness, however, has cursed you with a thousand questions!
Sitral is offline   Reply With Quote
Old 04-24-2002, 10:56 PM   #4
Koryon
Member
 
Join Date: Apr 2002
Location: Canada
Posts: 50
Koryon is on a distinguished road
Send a message via ICQ to Koryon Send a message via MSN to Koryon
Note; take this with a grain of salt, if at first you don't understand me, don't keep trying, find someone who makes more sense

Quote:
Originally Posted by
aside from "char ** storage" and the like. How does that differ from "char * storage"?
char ** storage is a pointer to pointer to char, confused yet? Good, when we have char x[blah] x is an array of fixed size, when we have char *x, x is a pointer to a memory location (of some not so fixed size).

But generally speaking, both of those x's are similar, you can subscript a pointer (put an array index on it) or you can read the offset from an array (x+1)... In more proper words, an array is a constant pointer that will always point to the beginning of an array, and all operations that can be done on an array can be done on a pointer and vice versa (as long as you don't try to change where an array is pointing at).

So, the point of me saying that overly-confusing blurb?

"char **storage" is a 2 dimensional array, sure it COULD be thought of as something more complicated, but you can make it simpler by thinking of it as a simple 2D array (and in unifex's code, it IS being used as a 2D array).

Also in unifex's code (sorry ) he mixes system memory functions (like free, malloc, calloc) with diku-like internal-to-program ones (strdup, free_string, etc). Mixing the two up is bad juju, nasty things may happen, possibly ending in a segfault which gdb insists is being caused by another part of your code, caveat hacker.
Koryon is offline   Reply With Quote
Old 04-25-2002, 06:23 AM   #5
Yui Unifex
Senior Member
 
Join Date: Apr 2002
Location: Florida
Posts: 323
Yui Unifex is on a distinguished road
Send a message via ICQ to Yui Unifex Send a message via AIM to Yui Unifex
Question

Quote:
Originally Posted by (Koryon @ blah)
he mixes system memory functions (like free, malloc, calloc) with diku-like internal-to-program ones (strdup, free_string, etc.
No he doesn't =P. str_dup() is a Diku function. strdup() is a BSD function, and I free it with free().
Yui Unifex is offline   Reply With Quote
Old 04-25-2002, 06:30 AM   #6
Seth
Senior Member
 
Join Date: Apr 2002
Location: Ede, The Netherlands
Posts: 271
Seth is on a distinguished road
Send a message via ICQ to Seth Send a message via MSN to Seth
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.
Seth is offline   Reply With Quote
Old 04-25-2002, 07:23 AM   #7
Koryon
Member
 
Join Date: Apr 2002
Location: Canada
Posts: 50
Koryon is on a distinguished road
Send a message via ICQ to Koryon Send a message via MSN to Koryon
Quote:
Originally Posted by
strdup() is a BSD function
Sorry, I new something didn't look right, never seen that before though
Koryon is offline   Reply With Quote
Old 04-26-2002, 07:42 AM   #8
Sitral
New Member
 
Join Date: Apr 2002
Posts: 11
Sitral is on a distinguished road
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.
Sitral is offline   Reply With Quote
Old 05-02-2002, 02:34 PM   #9
GenmaC
Member
 
Join Date: May 2002
Posts: 68
GenmaC is on a distinguished road
You could handle this with a double/single linked list too, but that's just the crack in my veins talking.
GenmaC is offline   Reply With Quote
Reply


Thread Tools


Help out a newbie? - Similar Threads
Thread Thread Starter Forum Replies Last Post
newbie crownrahul Tavern of the Blue Hand 1 02-04-2006 10:12 AM
Okay, this really is a newbie q. Rindu Newbie Help 1 12-24-2005 11:56 AM
Newbie Welcome... ozental Introduce Yourself 0 12-03-2004 11:27 AM
Newbie Looking for His Second MUD Walkingman Newbie Help 3 11-18-2003 01:30 PM
Newbie Demo Tavish Advanced MUD Concepts 9 04-28-2002 01:53 AM

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off

All times are GMT -4. The time now is 08:02 PM.


Powered by vBulletin® Version 3.6.7
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Style based on a design by Essilor
Copyright Top Mud Sites.com 2014