指向链表中指针的指针

我正在尝试通过指向指针的方式设置链表头。 我可以在函数内部看到头指针的地址正在改变但是当我返回主程序时它再次变为NULL。 谁能告诉我我做错了什么?

#include  #include  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 time = sec; new_timer->func = func; new_timer->next = NULL; while((*ppScan != NULL) && (((**ppScan).time)next; new_timer->next = *ppScan; *ppScan = new_timer; } 

由于C函数参数是通过它们的而不是通过它们的地址传递的,因此您不会传递调用中任何变量的地址:

 add_timer(time, func, head); 

所以在add_time函数范围之外都不会更改它们。

您可能需要做的是传递head的地址:

 add_timer(time, func, &head); 

和:

 void add_timer(int sec, fun_t func, TIMER_T **head) { TIMER_T ** ppScan = head; // ... } 

你得到了错误的方式。 该函数需要一个双指针, 调用者需要获取地址:

 { // caller TIMER_T *head = NULL; do_something(&head); } void do_something(TIMER_T ** p) // callee { *p = malloc(sizeof(TIMER_T*)); // etc. } 

之前有很多 很多相似的答案。