Mhlychol
Mhlychol

Reputation: 19

C Linked list, printf and "assignment makes integer from pointer without a cast" problem

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

Upvotes: 0

Views: 139

Answers (2)

dbush
dbush

Reputation: 224377

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

Upvotes: 3

Kurt Weber
Kurt Weber

Reputation: 176

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).

Upvotes: 1

Related Questions