0

I have this following code:

void      pushInList(t_chan **chan, char *name, char *nick_user) 
{
  t_chan        *new_channel;

  (void)nick_user;

  if ((new_channel = malloc(sizeof(t_chan))) == NULL)
    return ;
  new_channel->name = name;
  new_channel->prev = (*chan);
  (*chan) = new_channel;
} 

display_list(t_chan *chan, int fd)
{
  int           i;

  i = 0;
  while (chan != NULL)
   {
     printf("list : %s\n", chan->name);
     chan = chan->prev;
   }
}

int             create_chanel(int fd, char *buffer, t_chan **chan)
{
  char          *tmp;
  int           i;

  i = 0;
  if ((tmp = get_param(fd, buffer, 2, "NICK")) == NULL)
    return (EXIT_FAILURE);
  while ((*chan) != NULL)
   {
    /* FUTUR CHECK OF EXISTING CHANEL*/
    (*chan) = (*chan)->prev;
   }
  if ((*chan) == NULL)
    pushInList(chan, tmp, "Kilian");
  return (EXIT_SUCCESS);
}

int             main()
{
  t_chan        *chan;
  char          *test;

  chan = NULL;
  test = strdup("JOIN Coucou");
  create_chanel(4, test, &chan);
  test = strdup("JOIN Google");
  create_chanel(4, test, &chan);
  printf("-------------------\nlast display :\n");      
  display_list(chan, 4);
}

I don't understand why my list is every time NULL. I passed a pointer but in my main the list don't keep its values. Can you help me, I don't understand...

It's a chain list of channel, When a client send "Join Coucou", if the channel Coucou doesn't exist I create a new node.

Thank you in advance, Cordialy

wildplasser
  • 43,142
  • 8
  • 66
  • 109
Oraekia
  • 1,167
  • 2
  • 7
  • 14
  • 1
    in create_chanel :: `(*chan) = (*chan)->prev;` -->> `chan = &(*chan)->prev;` (and: you dont need the `if ((*chan) == NULL)`after the loop) – wildplasser Jun 10 '17 at 16:22
  • You may check my double linked list example (https://pastebin.com/mScMkkdy) It might be easier to understand how lists work. – 0andriy Jun 10 '17 at 17:32

0 Answers0