不兼容的指针类型和返回值使整数无强制转换问题

布雷特·哈塞尔

我正在尝试使用链表创建背包程序,但是我遇到了一个我很想知道的问题。在几乎所有的函数中,我都会在不兼容的指针类型的同一行上遇到此错误,我对如何解决它感到困惑。我碰到的另一个问题是一个return makes integer from pointer without a cast错误。以下是完整的代码和错误。错误在于knapsack.c文件中,test_knapsack.c仅出于测试目的给出主要功能示例。

错误

knapsack.c: In function ‘KnapsackRemove’:
knapsack.c:37:16: warning: return makes integer from pointer without a cast [-Wint-conversion]
         return NULL;
                ^
knapsack.c:47:29: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
                     knapsack=(*present)->next;

knapsack.c:59:12: warning: return makes integer from pointer without a cast [-Wint-conversion]
     return *knapsack;
            ^
knapsack.c: In function ‘KnapsackPrint’:
knapsack.c:69:17: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
             temp=(*temp)->next;
                 ^
knapsack.c: In function ‘KnapsackItemCount’:
knapsack.c:82:13: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
         temp=(*temp)-> next;
             ^
knapsack.c: In function ‘KnapsackSize’:
knapsack.c:94:13: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
         temp=(*temp)-> next;

背包

#include <stdio.h>
#include "knapsack.h"
#include <stdlib.h>

listitemptr KnapsackAdd(listitemptr *knapsack, int item){
    if(knapsack == NULL){
        listitemptr newer = (listitemptr)malloc(sizeof(listitemptr));
        newer-> item = item;
        newer-> count = 1;
        newer-> next = NULL;
        return newer;
    }else{
        listitemptr *temp = knapsack;
        listitemptr previous = NULL;
        while(temp!= NULL){
            if((*temp)->item == item){
                (*temp)->count=(*temp)->count+1;
                break;
            }
            previous = *temp;
            *temp = (*temp)->next;
        }
        if(temp == NULL){
            listitemptr newer = (listitemptr)malloc(sizeof(listitemptr));
            newer->item = item;
            newer->count =1;
            newer->next = NULL;
            previous->next = newer;
            return newer;
        }
        return *temp;
    }
}

int KnapsackRemove(listitemptr *knapsack, int item){
    if(knapsack==NULL)
        return NULL;
    listitemptr *present = knapsack;
    listitemptr previous = NULL;

    while(present != NULL){
        if((*present)->item == item){
            if((*present)-> count > 1){
                (*present)->count=(*present)->count-1;
            }else{
                if(previous==NULL){
                    knapsack=(*present)->next;
                }else{
                    previous->next=(*present)->next;
                    free(present);
                }
            }
            break;
        }
        previous=*present;
        *present=(*present)->next;
    }

    return *knapsack;
}

void KnapsackPrint(const listitemptr *knapsack){
    if(knapsack == NULL)
        printf("(nothing)\n");
    else{
       const listitemptr *temp = knapsack;
        while(temp!= NULL){
            printf("%d (%d), ",(*temp)->item, (*temp)->count);
            temp=(*temp)->next;
        }
        printf("\n");
    }
}

unsigned int KnapsackItemCount(const listitemptr *knapsack, int item){
    if(knapsack== NULL)
        return 0;
    const listitemptr *temp = knapsack;
    while(temp != NULL){
        if((*temp)-> item== item)
            return (*temp)-> count;
        temp=(*temp)-> next;
    }
    return 0;
}

unsigned int KnapsackSize(const listitemptr *knapsack){
    if(knapsack == NULL)
        return 0;
   const listitemptr *temp = knapsack;
    unsigned int sum = 0;
    while(temp!= NULL){
        sum+= (*temp)-> count;
        temp=(*temp)-> next;
    }
    return sum;
}

背包

/* knapsack.h
 * implements simple knapsack data structure as a linked list 
 * NOTE: a function may update the value of input argument *knapsack if it changes the first node of the knapsack to another node. Such a change include the case when an item is added to an empty knapsack
 */

typedef struct listitem* listitemptr;


struct listitem {
  int item;           // actual int item
  unsigned int count; // number of the same item in the knapsack; should be >= 1
  listitemptr next;   // pointer to next item 
};


listitemptr KnapsackAdd(listitemptr *knapsack, int item);


int KnapsackRemove(listitemptr *knapsack, int item);


void KnapsackPrint(const listitemptr *knapsack);


unsigned int KnapsackItemCount(const listitemptr *knapsack, int item);


unsigned int KnapsackSize(const listitemptr *knapsack);

test_knapsack.c

#include "knapsack.h"
#include <stdio.h>
#include <assert.h>

int main(){
   //knapsack k1
   listitemptr head=KnapsackAdd(NULL,10);
   KnapsackAdd(&head,-20);
   KnapsackAdd(&head,10);
   KnapsackAdd(&head,15);
   KnapsackAdd(&head,-20);
   KnapsackAdd(&head,10);

   KnapsackPrint(&head);

   int count=KnapsackItemCount(&head,10);
   assert(count==3);

   count=KnapsackItemCount(&head,8);
   assert(count==0);

   count=KnapsackSize(&head);
   assert(count==6);

   //knapsack2
   listitemptr head2=KnapsackAdd(NULL,5);
   KnapsackAdd(&head2,10);
   KnapsackAdd(&head2,15);
   KnapsackAdd(&head2,20);
   KnapsackAdd(&head2,-5);


   KnapsackPrint(&head2);

   KnapsackRemove(&head2,15);
   count=KnapsackSize(&head2);
   assert(count==4);

   count=KnapsackItemCount(&head2,15);
   assert(count==0);

   KnapsackRemove(&head2,10);
   count=KnapsackSize(&head2);
   assert(count==3);

   KnapsackAdd(&head,10);

   count=KnapsackSize(&head2);
   assert(count==3);

   count=KnapsackSize(&head);
   assert(count==7);   


   return 0;
   }
越来越白痴

看线

temp=(*temp)->next;

temp定义为const listitemptr *temp,表示其类型为const listitemptr *

要找到的类型(*temp)->next,我们可以看一下它的各个部分。我们已经知道temp是a const listitemptr *,因此取消引用它会导致listitemptr

看你的来源,我们可以再找个类型next

struct listitem {
  listitemptr next;   // pointer to next item 
};

现在我们可以看到next,因此(*temp)->next具有类型listitemptr

因此,您将分配listitemptrconst listitemptr *

看来这些错误中的大多数是由于混淆listitemptr指向的指针引起的listitemptr*

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

不兼容的类型:“整数”和“过程,无类型的指针或无类型的参数”

在c中创建单词搜索难题求解器,不兼容的指针类型和来自整数的指针,而在c中没有强制转换警告

强制转换返回强制类型的函数的返回值

如何处理“不兼容的返回值类型(重载函数)”问题

如何将double []转换为double,以获得返回值?(不兼容的类型错误)

错误:不兼容的类型:意外的返回值:Java的8

如果语句抛出不兼容的类型:意外的返回值

java.lang.IllegalStateException:返回值类型不兼容

指向整数转换的不兼容指针从结果类型为“NSInteger”(又名“long”)的函数返回“id _Nullable”

强制转换为兼容返回类型的并集是否满足函数指针的兼容性标准?

指针类型和常量不兼容

在 C 中使用指针:不兼容的整数到指针转换

将返回值强制转换为指针时,c中的内存分配

不兼容的类型错误:操作另一个类的方法的返回值

Dog.java:16:错误:不兼容的类型:意外的返回值

mypy抱怨:具有Type [TypeVar ['T',str,date]]和T输出的函数类型注释:返回值类型不兼容(获得了“ str”,期望的“ date”)

不兼容的指针类型

接收错误:赋值使指针从整数变为无强制转换

java.lang.IllegalArgumentException异常:无转换器发现类型的返回值

类型擦除后何时强制转换函数的通用返回值?

在不兼容类型问题上得到了.equals(),如何使ArrayList <Point>和点兼容,并将答案转换为布尔值

如何在LabVIEW中使用固有类型转换(在不兼容的指针/引用之间)强制断线?

Pascal指向指针SyntaxError和不兼容类型的指针

OpenCL:不兼容的整数到指针转换传递

C链表,printf和“赋值从指针进行整数运算而无强制转换”问题

C 上的字符和字符串赋值问题(来自指针的整数,没有强制转换?)

返回值无?

来自整数的冲突类型错误和指针没有强制转换警告 C

当函数只能返回str时,mypy抱怨返回值类型不兼容(得到“ Optional [str]”,预期为“ str”)