0

I tried compiling this code but its giving me "passing argument 2 of ‘strcpy’ makes pointer from integer without a cast [-Wint-conversion]". Can someone tell me what i need to do.

struct Book
{
char title[MAX_TITLE_LENGTH+1];  
char author[MAX_AUTHOR_LENGTH+1]; 
int  year;                       
};
void menu_delete_book(void)
{
char temp[300]; 
int x,b;
int no_books;
fgets(temp,MAX_TITLE_LENGTH,stdin);
{
for(x = 0; x< no_books; x++)
    {
    if (strcmp (book_array[x].title, temp) == 0)
        {
        for(x=1; x < no_books -1; x++)
            {
            for(b = x + 1; b < no_books; b++)
                {
                strcpy(book_array[x].title, book_array[b].title);   
                strcpy(book_array[x].author, book_array[b].author);
                strcpy(book_array[x].year, book_array[b].year);
                }
            }
        }
    }
}
no_books++;
John
  • 9
  • 1
  • Note that `fgets` retains the newline from the input buffer, so you should [remove that](https://stackoverflow.com/questions/2693776/removing-trailing-newline-character-from-fgets-input/28462221#28462221) before storing and comparing. – Weather Vane May 07 '18 at 21:34
  • 1
    Book::year is an integer, not a string, so `book_array[x].year = book_array[b].year` should work just fine. – jwdonahue May 07 '18 at 21:35
  • @John the proper way to thank is to "accept" the answer. – Weather Vane May 07 '18 at 21:52

1 Answers1

2

It's complaining about this line:

strcpy(book_array[x].year, book_array[b].year);

Book::year is an integer, not a string. Do this instead:

book_array[x].year = book_array[b].year;

The warning is telling you that the integer values you were passing to strcpy were being implicitly converted to pointers. It's almost always an error to convert int to pointer and in your case, the integers definitely don't contain valid pointer values.

jwdonahue
  • 6,199
  • 2
  • 21
  • 43