简体   繁体   English

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

[英]Incompatible pointer type and return makes integer without a cast issue

I'm attempting to create a knapsack program out of the use of linked list but ive run into an issue that i can quite figure out. 我正在尝试使用链表创建背包程序,但我遇到了一个我很想知道的问题。 In nearly all of my functions im getting this error on the same line of an incompatible pointer type and i'm confused on how to fix it. 在几乎所有的函数中,我都会在不兼容的指针类型的同一行上遇到此错误,而我对如何解决它感到困惑。 The other issue im running into is a return makes integer from pointer without a cast errors. 我碰到的另一个问题是return makes integer from pointer without a cast错误。 Full code and errors below. 以下是完整的代码和错误。 The error lies in the knapsack.c file, test_knapsack.c is just given for a main function example for testing purposes. 该错误位于knapsack.c文件中, test_knapsack.c仅作为用于测试目的的主要函数示例给出。

ERRORS 错误

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;

knapsack.c knapsack.c

#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 knapsack.h

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

Regarding the warning - 关于警告-

knapsack.c:37:16: warning: return makes integer from pointer without a cast [-Wint-conversion]
     return NULL;

The definition of NULL is compiler dependent, probably in your compiler NULL is defined as (void*)0 , therefore you are trying to return a pointer but your function is defined as int KnapsackRemove(...) so it expects an int value to be returned. NULL的定义取决于编译器,可能是在您的编译器中NULL被定义为(void*)0 ,因此您试图返回一个指针,但是您的函数被定义为int KnapsackRemove(...)因此它期望一个int值被退回。

Regarding - 关于-

knapsack.c:59:12: warning: return makes integer from pointer without a cast [-Wint-conversion]
 return *knapsack;

You are trying to return a listitemptr type from a function returning an int. 您正在尝试从返回int的函数中返回listitemptr类型。

regarding the other warnings, you are trying to assign listitemptr to a listitemptr* type. 关于其他警告,您正在尝试将listitemptr分配给listitemptr*类型。 (*present)->next is of type listitemptr and knapsack is of type listitemptr* . (*present)->next是类型的listitemptrknapsack的类型为listitemptr*

Looking at the line 看线

temp=(*temp)->next;

temp is defined as const listitemptr *temp , meaning its type is const listitemptr * . temp定义为const listitemptr *temp ,表示其类型为const listitemptr *

To find the type of (*temp)->next , we can look at its pieces. 要找到(*temp)->next ,我们可以看一下它的片段。 We already know temp is a const listitemptr * , so de-referencing it results in a listitemptr . 我们已经知道temp是一个const listitemptr * ,因此取消引用它会导致一个listitemptr

Looking at your source, we can then find the type of next , 查看您的来源,然后我们可以找到next的类型,

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

now we can see that next , and therefore (*temp)->next , has the type listitemptr . 现在我们可以看到next ,因此(*temp)->next的类型为listitemptr

So you are assigning a listitemptr to a const listitemptr * . 因此,您正在将listitemptr分配给const listitemptr *

It looks like most of these errors are caused by confusing listitemptr for a pointer to listitemptr* . 这些错误中的大多数似乎是由于混淆listitemptr和指向listitemptr*的指针引起的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM