|
|||||||
This is a discussion on "Help out a newbie?" in the Top Mud Sites MUD Coding forum : 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 ... |
|
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our MUD community today! If you have any problems with the registration process or your account login, please contact us. If you are a registered member of the old TMS forums, please click here
|
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 |
|
New Member
Join Date: Apr 2002
Posts: 11
![]() |
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. |
|
|
|
|
|
#2 |
|
Senior Member
|
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. |
|
|
|
|
|
#3 |
|
New Member
Join Date: Apr 2002
Posts: 11
![]() |
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! |
|
|
|
|
|
#4 | |
|
Member
|
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:
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 |
|
|
|
|
|
|
#5 | |
|
Senior Member
|
Quote:
|
|
|
|
|
|
|
#6 |
|
Senior Member
|
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.
|
|
|
|
|
|
#7 | |
|
Member
|
Quote:
|
|
|
|
|
|
|
#8 |
|
New Member
Join Date: Apr 2002
Posts: 11
![]() |
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. |
|
|
|
|
|
#9 |
|
Member
Join Date: May 2002
Posts: 68
![]() |
You could handle this with a double/single linked list too, but that's just the crack in my veins talking.
|
|
|
|
![]() |
| 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 |
|
|