错误:参数的类型不兼容

我正在用C写一个列表。以下是来源:

#include  #include  struct list { int value; struct list *next; }; typedef struct list ls; void add (ls **head, ls **tail, int val) { ls *new, *tmp1, *tmp2; if (NULL == *head) { new = (ls*)malloc(sizeof(ls)); *head = new; *tail = new; new->value = val; new->next = NULL; return; } else { tmp1 = *head; tmp2 = tmp1->next; while (tmp2 != NULL) { tmp1 = tmp2; tmp2 = tmp1->next; } new = (ls*)malloc(sizeof(ls)); new->value = val; new->next = NULL; *tail = new; return; } } void show (ls **head, ls **tail) { int i; ls *tmp; while (tmp->next != NULL) { printf("%d: %d", i, tmp->value); i++; tmp=tmp->next; } return; } int main (int argc, char *argv[]) { ls *head; ls *tail; int n, x; head = (ls*)NULL; tail = (ls*)NULL; printf("\n1. add\n2. show\n3. exit\n"); scanf("%d", &x); switch (x) { case 1: scanf("%d", &n); add(*head, *tail, n); break; case 2: show(*head, *tail); break; case 3: return 0; default: break; } return 0; } 

当我用gcc编译它

 gcc -o lab5.out -Wall -pedantic lab5.c 

我得到奇怪的错误:

 lab5.c: In function 'main': lab5.c:84:3: error: incompatible type for argument 1 of 'add' lab5.c:16:6: note: expected 'struct ls **' but argument is of type 'ls' lab5.c:84:3: error: incompatible type for argument 2 of 'add' lab5.c:16:6: note: expected 'struct ls **' but argument is of type 'ls' lab5.c:88:3: error: incompatible type for argument 1 of 'show' lab5.c:52:6: note: expected 'struct ls **' but argument is of type 'ls' lab5.c:88:3: error: incompatible type for argument 2 of 'show' lab5.c:52:6: note: expected 'struct ls **' but argument is of type 'ls' 

对我来说一切都好……

参数类型是ls**而不是编译器所说的ls

有人看到什么可能是错的?

PS。 我知道没有必要将*tail作为参数并且它是未使用的,但它会是,因为我想开发这个’程序’……

正如丹尼尔在他的评论中所说,并且在他的回答中说,使用&而不是*会给你你想要的东西。 这里有一点解释,帮助你记住。

* 取消引用它附加的内容,这意味着它将变量视为指针并在该地址处给出值。

&传递引用 ,意味着它给出了值存储在的地址,或者更确切地说,将指针传递给变量。

由于你想传​​递一个指向变量head和tail的指针,你想把它们作为&head&tail传递给它,它会在另一端给你**head**tail的值。 在你习惯它之前,这似乎是违反直觉的。

 add(*head, *tail, n); 

应该 :

 add(&head, &tail, n); 

因为你需要将head和tail指针的地址传递给函数。

需要对show函数的调用进行类似的修复。