0

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;
}
2
  • You can't assign arrays this way. new->bookname[50] is a character type, and you're trying to assign a pointer to a character. Loop over 1 to 50 and copy bookname[i] to new->bookname[i] Commented Dec 10, 2020 at 2:15
  • bookname[50] is 11 elements beyond the last valid index in char bookname[40]; (in addition to all the other reason the code is wrong) Commented Dec 10, 2020 at 3:32

2 Answers 2

3

On these lines:

    new_node->bookname[50]= bookname;
    new_node->writer[50]= writer;

You think you're copying the contents of one array to another. What you're actually doing is attempting to copy a pointer (since arrays as function arguments decay to pointers) to a single element of the array (i.e. a char), and to an element past the end of the array at that.

Even if you removed the index from the destination, this wouldn't work because arrays are not assignable.

To copy one string to another, use strcpy:

    strcpy(new_node->bookname, bookname);
    strcpy(new_node->writer, writer);
Sign up to request clarification or add additional context in comments.

1 Comment

Not to mention to attempt to assign to indexes beyond the bounds of the array. Array is not an lvalue cite is C11 Standard - 6.3.2.1 Other Operands - Lvalues, arrays, and function designators(p3)
1

Although arrays and pointers to arrays can be treated as interchangeable in some cases, this is not one of them. Since bookname and writer are declared as char arrays and not char pointers, you won't be able to accomplish what you're trying to do by assigning a char * to either of them. Instead, you should use strncpy() (assuming these are null-terminated strings) to copy them over.

You should also double-check the array indices--you're trying to copy to bookname[50] (which would be the 51st element, because of zero-based numbering) while bookname[]'s declaration is only for 40 elements (for the same reason of zero-based index numbering, there is no writer[50] even though it is declared as char writer[50] -- the declaration creates a 50-element array, whose first element is at index 0 and fiftieth at 49).

2 Comments

thank you, I have one more question; what can i do to get bookname and writer get with scanf ?
I edit the question and add the code I try to write the last

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.