pointer to a pointer in a linked list

Posted by user1596497 on Stack Overflow See other posts from Stack Overflow or by user1596497
Published on 2012-08-15T08:51:43Z Indexed on 2012/09/26 9:38 UTC
Read the original article Hit count: 166

Filed under:
|
|

I'm trying to set a linked list head through pointer to a pointer. I can see inside the function that the address of the head pointer is changing but as i return to the main progran it becomes NULL again. can someone tell me what I'm doing wrong ??

           #include <stdio.h>
           #include <stdlib.h>

           typedef void(*fun_t)(int);
           typedef struct timer_t {
           int   time;
           fun_t func;
           struct timer_t *next;
           }TIMER_T;

          void add_timer(int sec, fun_t func, TIMER_T *head);

             void run_timers(TIMER_T **head);

           void timer_func(int);

           int main(void)
            {
            TIMER_T *head = NULL;
            int time = 1;

            fun_t func = timer_func;

            while (time < 1000) {
              printf("\nCalling add_timer(time=%d, func=0x%x, head=0x%x)\n", time,     

              func, &head);
               add_timer(time, func, head);
               time *= 2;
              }  
              run_timers(&head);

              return 0;
             }

            void add_timer(int sec, fun_t func, TIMER_T *head)
            {
           TIMER_T ** ppScan=&head;
               TIMER_T *new_timer = NULL;
           new_timer = (TIMER_T*)malloc(sizeof(TIMER_T));
               new_timer->time = sec;
               new_timer->func = func;
               new_timer->next = NULL;

               while((*ppScan != NULL) && (((**ppScan).time)<sec))
               ppScan = &(*ppScan)->next;

               new_timer->next = *ppScan;
               *ppScan = new_timer;
               } 

© Stack Overflow or respective owner

Related posts about c

    Related posts about pointers