0

Here is the piece of code: I wonder where my mistake is? I have a structure named country, working with linked lists, and here is my function for searching:

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

In main I have (k is a int variable):

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 ");

When I compile this in Dev C++, I get:

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

How do I fix this problem?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
aB61
  • 17
  • 1
  • 6
  • 2
    Instead of assigning the return value of `search` to a variable of type `int`, create a variable of type `country*` and assign the return value of `search` to that. – R Sahu Mar 08 '15 at 07:02
  • 2
    Please learn to use a more orthodox code layout; your code fragments are almost unreadable and very hard to comprehend because the layout is extraordinary — especially the first fragment. Also, beware of [using `fflush(stdin)`](http://stackoverflow.com/questions/2979209/using-fflushstdin) and [why the `gets()` function cannot be used safely](http://stackoverflow.com/questions/1694036/why-is-the-gets-function-dangerous-why-should-it-not-be-used). – Jonathan Leffler Mar 08 '15 at 07:10
  • 1
    As @RSahu said, your function `search()` is declared to return a `country *`, so assigning its result to `k`, an `int`, is bound to give the error. Return 0 or NULL for a missing value when the non-missing return type is a pointer. Returning `(void *)-1` is aconventional (though not without standardized precedent). – Jonathan Leffler Mar 08 '15 at 07:12
  • What did you expect `k` to contain after running `k = search(v)`? – user253751 Mar 08 '15 at 08:04
  • Doesn't the error messages together with the code line it issues says it all? – alk Mar 08 '15 at 08:41
  • @immibis I expected an integer, I wondered it would be ok if k would contain the length of my string... or something like that – aB61 Mar 08 '15 at 09:39
  • @alk the line of the code is: k = search(v); – aB61 Mar 08 '15 at 10:03
  • @aB61 but how can it be an integer if `search` doesn't return an integer? – user253751 Mar 08 '15 at 10:15
  • I know it is not possible... my mistake – aB61 Mar 08 '15 at 10:25

1 Answers1

3

Couple of things to fix:

  1. The return value of search when you don't find what you are searching for:

    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. Use the right variable type to assign the return value of search.

    // 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 ");
    }
    
R Sahu
  • 204,454
  • 14
  • 159
  • 270