错误:参数类型不兼容 [英] error: incompatible type for argument

查看:28
本文介绍了错误:参数类型不兼容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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;
}

当我用 gcc 编译时

When I compile it with 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’

对我来说一切都很好...

For me everything is OK...

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

And argument type is ls** and not ls as compiler says.

有人知道哪里出了问题吗?

Someone see what can be wrong?

附注.我知道没有必要给 *tail 作为参数,它没有被使用,但是它会是,因为我想开发这个程序"......

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'...

推荐答案

正如 Daniel 在他的评论中所说和 codacci 在他的回答中所说的那样,使用 & 而不是 *会给你你想要的.不过,这里有一些解释,以帮助您记住.

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 - 引用它所附加的内容,这意味着它将变量当作一个指针并给出该地址处的值.

* 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.

由于您想传递指向变量 head 和 tail 的指针,因此您希望将它们作为 &head&tail 传递,这将为您提供值**head**tail 在另一端.在您习惯之前,这似乎违反直觉.

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.

这篇关于错误:参数类型不兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆