问题描述
#include< stdio .H>
#include< stdlib.h>
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->下;
while(tmp2!= NULL)
{
tmp1 = tmp2;
tmp2 = tmp1->下;
}
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->下一个;
}
return;
}
int main(int argc,char * argv [])
{
ls * head;
ls * tail;
int n,x;
head =(ls *)NULL;
tail =(ls *)NULL;
printf(\\\
1。add\\\
2。show\\\
3。exit\\\
);
scanf(%d,& x);
switch(x)
{
case 1:
scanf(%d,& n);
add(* head,* tail,n);
休息;
案例2:
show(* head,* tail);
休息;
案例3:
返回0;
默认值:
break;
}
返回0;
}
当我使用gcc进行编译时
gcc -o lab5.out -Wall -pedantic lab5.c
$
lab5.c:在函数'main'中:
lab5 .c:84:3:error:'add'参数1的不兼容类型
lab5.c:16:6:note:预期'struct ls **'但参数类型为'ls'
lab5.c:84:3:error:'add'参数2的不兼容类型
lab5.c:16:6:注意:期望'struct ls **'但参数类型为'ls'
lab5.c:88:3:error:'show'参数1的不兼容类型
lab5.c:52:6:note:expected'struct ls **'但参数类型为'ls '
lab5.c:88:3:error:'show'参数2的不兼容类型
lab5.c:52:6:note:expected'struct ls **'但参数类型'ls'
对我而言,一切都很好...
参数类型是 ls **
而不是 ls
就像编译器说的那样。
有人看到什么可能是错误的?
PS。我知道没有必要给 * tail
作为参数,但它是未使用的,因为我想开发这个'程序'...
&
代替的 *
会给你你想要的。以下是一些解释,以帮助您记忆。 *
de - 引用它所附带的内容,意味着它将变量当作一个指针并给出该地址的值。
&
传递引用,这意味着它给出了该值存储的地址,或者更确切地说,将一个指针传递给变量。
既然你想传递一个指向变量头部和尾部的指针,你想把它们作为& head
和& tail
,它给你在另一端的值 **头
和 **尾
。在你习惯之前,它似乎是反直觉的。
I'm writing a list in C.Below is the source:
#include <stdio.h>
#include <stdlib.h>
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;
}
When I compile it with gcc
gcc -o lab5.out -Wall -pedantic lab5.c
I get strange errors:
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’
For me everything is OK...
And argument type is ls**
and not ls
as compiler says.
Someone see what can be wrong?
PS. I know that it's unnecessary to give *tail
as argument and it's unused, however it will be, because I want to develop this 'program'...
As Daniel said in his comment and codaddict said in his answer, using &
instead of *
will give you what you want. Here's a bit of an explanation though, to help you remember.
*
de-references what it's attached to, meaning it takes the variable as if it's a pointer and gives the value at that address.
&
passes the reference, meaning it gives the address that the value is stored at, or rather, passes a pointer to the variable.
Since you want to pass a pointer to the variables head and tail, you want to pass them as &head
and &tail
, which gives you the values **head
and **tail
at the other end. It seems counter-intuitive until you get used to it.
这篇关于错误:参数的不兼容类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!