Re: Problems with Linked Lists

From: Leonardo Herrera L. (leherrer@ENTELCHILE.NET)
Date: 09/25/97


Rasdan wrote:
>
> So, I have done this:
>
> struct player_index_element {
>   char *name;
>   int id;
>
>   struct player_index_element *next;
> }
>
> Then basically, did this:
>
> void load_player_index(void)
> {
[...]
>     create_pfile_entry(&pie);
>   }
>   fclose(file);
> }

> void create_pfile_entry(struct player_index_element *pie)
> {
>   struct player_index_element *pindex;
>
>   pie->next = NULL;
>
>   CREATE(pindex, struct player_index_element, 1);
>
>   *pindex = *pie;

Do you want to copy the data here?

With this, you are copying only the first four bytes (or something) of
your pointer. If your structure is

struct player_index_element {
  struct player_index_element *next;

  char *name;
  int id;
}

then you get a complete linked list, but with all the data screwed... no
names and no ids, of course.

The better way to do this is using the memcpy function:

void *memcpy(void *dest, const void *src, size_t n);


void create_pfile_entry(struct player_index_element *pie)
{
  struct player_index_element *pindex;

  pie->next = NULL;

  CREATE(pindex, struct player_index_element, 1);

  memcpy( pindex, pie, sizeof( struct player_index_element ) );

  pindex->next = player_table;
  player_table = pindex;
}

(well, i supose that player_table is a struct player_index_element * )

Tell me if this works.

Bye!

P.S.: Sorry for my english...
--
Leonardo Herrera L.
mailto:leherrer@entelchile.net
  "Me voy a subai, me voy a costai, me voy a tapai y
me voy a hacei tuto."
        -- Ruy


     +------------------------------------------------------------+
     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     | http://democracy.queensu.ca/~fletcher/Circle/list-faq.html |
     +------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/08/00 PST