I was looking through the str's in db.c...

Posted by Nick Cash on Thu 06 Feb 2003 02:16 AM — 3 posts, 9,171 views.

USA #0
I was looking through the str functions (e.g. str_cmp, str_prefix, etc.) and I found str_dup. I've noticed it before in other files, so I was curious to see what it did. The comment before it offer's no explanation, unlike str_cmp, so I am still wonder. It may be a small code, but its used enough it my code to get me to ask. Can anyone explain to me the significance of str_dup?

Thanks.
USA #1
Also another thing, if you would be so kind to answer it for me. I'm writing a code that calls for the use of switch(). I was looking through some examples in other codes and I noticed that a lot of the use ch->dest_buf and ch->dest_buf2. What are these used for, I see them mostly in swskills.c, which is kind of making items, so it might have to do with that. Anyways, if one of you really smart and experienced coders could answer my newbie questions that would be great.

Thanks for everything.
Australia Forum Administrator #2
str_dup duplicates a string. C doesn't have really elegant string handling built in, so you normally represent strings by allocating memory (using malloc) and then copying the data into the allocated memory. You then refer to the string by a pointer to its allocated memory.

Thus, you can't just make a copy of a string by copying the pointer, as you would have two pointers pointing to the same piece of memory, and if you changed or deleted one, you would change or delete both.

str_dup allocates another batch of memory, and then copies the original string into it, thus duplicating the string.

As for ch->dest_buf2 it is hard to say without seeing the code, but sounds like a "destination buffer".