-2

I have problem with my code , I get run time error.

This code is suppose to ask the user to enter a name and then go through the file and search for the input entered by the user.

and I got this warning.

( [Warning] passing argument 1 of 'strcmp' makes pointer from integer without a cast [enabled by default]

void search()
{
 FILE *infile;
int i=0,found=0;
char list[SIZE], str[SIZE];

infile = fopen("records.txt","r");
printf("enter a book name to search in the list > /n");
gets(list);
i=0; 
while (!found && i<SIZE) {
        if(strcmp(list[i], str) == 0)
            found = 1;
        else
          i++;
}

if(found)
        printf("%s is in the list at row %d\n",str,i);
    else printf("%s is not in the list.\n", str);

  printf("\n");    
}
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • 1
    `list` should be either [`char* list[]` or `char list[][] `](http://stackoverflow.com/questions/17564608/what-does-the-array-name-mean-in-case-of-array-of-char-pointers/17661444#17661444) – Grijesh Chauhan May 10 '15 at 16:31

1 Answers1

1

The problem here is with the usage of strcmp(). As per the man page,

int strcmp(const char *s1, const char *s2);

it expects a const char * as both of the arguments. But you're passing list[i], which is a char.

Also, never use gets(). It's very unsafe, use fgets() instead.

That said, i think your program is seriously broken.

  1. you opened the file, never read anything from it. Why?
  2. the str is used uninitialized. Lead to Undefined behavior.
  3. You never closed the file.
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • good answer, but OP also don't know how to read file, declaration of list is also incorrect. – Grijesh Chauhan May 10 '15 at 16:41
  • @GrijeshChauhan You're very right sir, but that is not the scope of this question, IMHO. That is why, I added last three bullets. I addressed the question present in the title. :-) – Sourav Ghosh May 10 '15 at 16:46