I try to create a library program with a linked list in C. I get an "assignment makes integer from pointer without a cast" error and when I try to print screen the list print_list function not working; only printf "H->" doesn't print in the while loop.
#include<stdio.h>
#include<stdlib.h>
struct node
{
char bookname[40];
char writer[50];
int available;
int memberid;
struct node *next;
};
void AddBook(struct node **head, char bookname[50], char writer[50], int available, int memberid)
{
struct node * new_node = NULL;
struct node * last = NULL;
new_node = (struct node *)malloc(sizeof(struct node));
if (new_node == NULL)
{
printf("Failed to insert element. Out of memory");
return;
}
new_node->bookname[50]= bookname;
new_node->writer[50]= writer;
new_node->available= available;
new_node->memberid= memberid;
new_node->next = NULL;
At this point, I get an "assignment makes integer from pointer without a cast" problem on these two;
new_node->bookname[50]= bookname;
new_node->writer[50]= writer;
if( *head == NULL)
{
*head = new_node;
return;
}
last = *head;
while(last->next) last = last->next;
last->next = new_node;
}
void print_list(struct node *head)
{
printf("H->");
while(head)
{
printf("%s %s %d %d ->", head->bookname[50],head->writer[50],head->available,head->memberid);
head = head->next;
}
printf("|||\n\n");
}
int main()
{
struct node * head = NULL;
AddBook(&head,"Hamlet","William_Shakespeare",1,1);
AddBook(&head,"The Odyssey","Homer",1,1);
AddBook(&head,"The Great Gatsby","F. Scott Fitzgerald",1,1);
print_list(head);
return 0;
}
What can I do to get book name and writer get with scanf? I tried to do it this way but it didn't work;
int main()
{
struct node * head = NULL;
struct node book;
prinft("please enter the bookname:");
scanf("%s", book.bookname);
prinft("\n please enter the writer name:"); scanf("%s",book.bookname);
book.available=1;
book.memberid=1;
AddBook(&head,*book.bookname,*book.writer,book.available,book.memberid);
print_list(head);
return 0;
}
new->bookname[50]is a character type, and you're trying to assign a pointer to a character. Loop over 1 to 50 and copybookname[i]tonew->bookname[i]bookname[50]is11elements beyond the last valid index inchar bookname[40];(in addition to all the other reason the code is wrong)