c警告-传递‘的参数1;插入’;来自不兼容的指针类型

c警告-传递‘的参数1;插入’;来自不兼容的指针类型,c,pointers,struct,insert,compiler-warnings,C,Pointers,Struct,Insert,Compiler Warnings,我是C编程的新手。我必须为家庭作业编写一些函数/程序,然后用教授的make测试进行测试 我有一个包含两个元素的结构:第一个元素是指向列表的指针,第二个元素是列表排序所依据的顺序类型。以下是数据结构: /* order type NOTORD = no order TIME = ordered by increasing time POSITION = ordered by increasing position */ typedef enum {NOTORD=0, TIME=1, POSITIO

我是C编程的新手。我必须为家庭作业编写一些函数/程序,然后用教授的make测试进行测试

我有一个包含两个元素的结构:第一个元素是指向列表的指针,第二个元素是列表排序所依据的顺序类型。以下是数据结构:

/* order type
NOTORD = no order
TIME = ordered by increasing time
POSITION = ordered by increasing position */
typedef enum {NOTORD=0, TIME=1, POSITION=2} ord_t;

typedef struct elem {
  double position;
  double time;
  struct elem * next;
} elem_t;

typedef struct {
  elem_t * head;
  ord_t ord; 
} lista_t;
我必须根据顺序的类型在列表中插入一个元素(由输入给出)。这是我的密码:

void inserisciInTesta(lista_t *l, elem_t *el) {
    el->next=l->head;
    l->head=el;
}

void inserisciTime(lista_t * l, elem_t* pelem) {
   if(l->head->time >= pelem->time)
       inserisciInTesta(l, pelem);
   else
       inserisciTime(l->head->next, pelem);
}

void inserisciPosition(lista_t * l, elem_t* pelem) {
    if((l->head)->position >= pelem->position)
        inserisciInTesta(l, pelem);
    else
        inserisciPosition(l->head->next, pelem);
}

int inserisci(lista_t * l , elem_t* pelem) {  
    if(l->ord==TIME) {
        inserisciTime(l, pelem);
        return 0;
}
    else if(l->ord==POSITION){
        inserisciPosition(l, pelem);
        return 0;
}
    else {
        inserisciInTesta(l, pelem);
        return 0;
    }
}
进行测试时,我收到以下警告:

raggi.c:在函数“inserisciTime”中:

raggi.c:42:27:警告:从不兼容的指针类型[-Wincompatible指针类型]传递'InseriscItem'的参数1

        inserisciTime(l->head->next, pelem);
                      ^
        inserisciPosition(l->head->next, pelem);
                          ^
raggi.c:38:6:注意:应为'lista_t*{aka struct*}',但参数的类型为'struct elem*'

无效插入时间(列表、元素){ ^

raggi.c:在函数“insisiposition”中:

raggi.c:49:31:警告:从不兼容的指针类型[-Wincompatible指针类型]传递参数1“inseriscposition”

        inserisciTime(l->head->next, pelem);
                      ^
        inserisciPosition(l->head->next, pelem);
                          ^
raggi.c:45:6:注意:应为'lista_t*{aka struct*}',但参数的类型为'struct elem*'

虚空插入位置(列表、元素){ ^


我的代码有什么问题?谢谢。

< p>编译器警告非常多。请考虑第一个。<代码> iSeTimeStudio< /Cl>期待一个<代码> ListaT**/Cl>作为其第一个参数。坏的调用是通过<代码> L> >头>下< /代码>。那是什么类型?好,<代码> L>代码>是<代码> ListaTy**/Clult>o
l->head
是一个
elem\u t*
。因此
l->head->next
是一个
struct elem*
,相当于
elem\u t*
。这是一个类型不匹配:函数需要一个
lista\u t*
,而您正在传递一个
elem\u*

其中一个必须更改:要么更改函数以匹配所传递的内容,要么更改所传递的内容以匹配函数所期望的内容。编译器警告将您保存在此处,因为它不可能按原样工作


另一个警告也有完全相同的问题。

错误的原因是Tom解释的。我建议您更改函数,而不是使用递归调用(即函数调用本身),而是使用while循环来查找需要插入元素的位置

考虑到在插入新元素时需要修改两个指针:指向下一个元素的指针和来自上一个元素的指针。使用递归函数执行此操作可能会更加混乱


另外,检查在列表开头、末尾插入的特殊情况,以及列表是否为空。

警告不是很清楚:“
参数1…不兼容指针类型”
”?还有十几个标题几乎相同的问题,这就是警告。这是重复的。