On Mon, 13 Jan 2003, Brett Phipps wrote: > > > void add_trophy (CHAR_DATA *ch, CHAR_DATA *victim) > > > { > > > int i = 0; > > > TROPHY_DATA * trophy; > > > > > > /* If were the first entry, set it up */ > > > if (ch->pcdata->trophy_list->next == NULL)
[snipped] > > > strcat(ch->pcdata->trophy->short_desc, victim->short_descr); > > ^^^^^^ > >Big no no. Don't ever do this unless you have a large character > >buffer (you don't here. All you have is a pointer) > > Thanks for the heads up. When dealing with copying strings of this > nature, what is the prefered method to do it? You have to allocate memory before you can put anything in it. One way to do it would be call malloc(strlen(original_string) + 1) before using strcat. However, there's already a C library function strdup() that does just this. And ROM has a duplicate, str_dup() of this function that uses ROM's memory management. The way to do this would be: ch->pcdata->trophy->short_desc = str_dup(victim->short_descr); If ch->pcdata->trophy->short_desc already pointed to a string, you'll want to call free_string(ch->pcdata->trophy->short_desc) first. You're setting it to ROM's "empty" string when you create a new trophy structure, so this applies here. > It would seem to me at least that keeping a seperate empty "start of > list" variable would be the easiest way to keep track of things. > But I could very well be wrong on this as well. At any rate, thanks for the > response. I will be keeping on this until I get it working properly, so > expect another question or two in the coming days. This really just depends on your preference. Using just a NULL pointer for an empty list, instead of an empty "start of list" variable is the most memory effiecient way to handle linked lists. However, you have to treat the beginning of the list specially when inserting/deleting. Using an empty "start of list" variable will use more memory (the size of one node in the list is "wasted". But it allows the code to handle the linked list to be simpler. The head of the list is not a special case that has to be handled separately when inserting/deleting. In my experience, using a NULL pointer for the head of the list is the most common method of implementing a linked list, but using a sentinel head node does have its advantages. Dennis