如何解决错误"assignment makes integer from pointer without a cast"?

How to solve the error "assignment makes integer from pointer without a cast"?

这是一段代码:我想知道我的错误在哪里? 我有一个名为 country 的结构,使用链表,这是我的搜索函数:

country *search(char *v)
{
   country *c; c=head;
   while(c)
   {
           if(strcmp(c->name,v)==0)
           {return c;}
           c=c->next;
           if(c==head) break;}
           return (void *)-1;}

main 我有(k 是一个 int 变量):

printf("  \n\tEnter name of country for searching:   ");
                fflush(stdin); gets(v);
                k = search(v); // Function for searching
                if(k>=0)
                {puts("\n\t Info about Country: \n ");

当我在 Dev C++ 中编译时,我得到:

[Warning] assignment makes integer from pointer without a cast [enabled by default]

我该如何解决这个问题?

有几件事需要解决:

  1. 当您没有找到您要搜索的内容时,search 的 return 值:

    country *search(char *v)
    {
       country *c; c=head;
       while(c)
       {
          if(strcmp(c->name,v)==0)
          {
             return c;
          }
          c=c->next;
          if(c==head) break;
       }
    
       // Don't use -1 as an invalid search.
       // Use NULL instead.
       // return (void *)-1;
       return NULL;
    }
    
  2. 使用正确的变量类型为search赋值return。

    // Don't use k, which is of type int.
    // Use a variable of the right type.
    // k = search(v);
    country* cPtr = search(v);
    if( cPtr )
    {
       puts("\n\t Info about Country: \n ");
    }