1

i have a problem changing the struct that is an array.

I'm working on a library project in C language that need to add a book (Book struct) into a library, I have an array that has all my books, I need to add to this array, my new book.

i did this, could anyone help me , and give me a little bit information about it ?

#include <stdio.h>
#include <string.h>

#define BOOK_NUM 50
#define NAME_LENGTH 200
#define AUTHOR_NAME_LENGTH 100
#define PUBLISHER_NAME_LENGHT 50
#define GENRE_LENGHT 50

typedef struct _Book
    {
    char name[NAME_LENGTH];
    char author[AUTHOR_NAME_LENGTH];
    char publisher[PUBLISHER_NAME_LENGHT];
    char genre[GENRE_LENGHT];
    int year;
    int num_pages;
    int copies;
}Book;

Book books_arr[BOOK_NUM],*ptr=books_arr;

void add_book()
{
    char book_name[NAME_LENGTH],author_name[AUTHOR_NAME_LENGTH],publisher_name[PUBLISHER_NAME_LENGHT],book_genre[GENRE_LENGHT];
    int book_year,book_pages,book_copies,cnt=0,cnt2=0;
    printf("Please enter book name:\n");
    scanf("%s",&book_name);
    printf("Please enter author name:\n");
    scanf("%s",&author_name);
    printf("Please enter publisher name:\n");
    scanf("%s",&publisher_name);
    printf("Please enter book genre:\n");
    scanf("%s",&book_genre);
    printf("Please enter the year of publishment:\n");
    scanf("%d",&book_year);
    printf("Please enter the number of pages:\n");
    scanf("%d",&book_pages);
    printf("Please enter the number of copies:\n");
    scanf("%d",&book_copies);
    for (ptr=books_arr;ptr<&books_arr[BOOK_NUM];ptr++)
    {
        if (strcmp(book_name,(*ptr).name)==0)
            (*ptr).copies=(*ptr).copies+book_copies;
        if(strcmp(book_name,(*ptr).name)!=0)
            cnt++;
        if((*ptr).name!=NULL)
            cnt2++;
    }
    if(cnt==BOOK_NUM)
    {
        if(cnt2==BOOK_NUM)
            printf("There is no place in the library for this book\n");
        if(cnt2<BOOK_NUM)
        {
            (*ptr).name=book_name;
            (*ptr).author=author_name;
            (*ptr).publisher=publisher_name;
            (*ptr).genre=book_genre;
            (*ptr).year=book_year;
            (*ptr).num_pages=book_pages;
            (*ptr).copies=book_copies;
        }
    }
}

every time i compile the code, i have the problem, "expression must be a modifiable lvalue".

thank you

3
  • When you have an error, identify the line where the error is reported. We shouldn't have to guess and work it out for ourselves. Commented Dec 27, 2016 at 4:08
  • Possible duplicate of problems with char array = char array Commented Dec 27, 2016 at 4:09
  • 1
    The arrow operator -> was invented to make it easier to write code like ptr->member rather than having to use the (*ptr).member. Commented Dec 27, 2016 at 4:13

2 Answers 2

1

You're attempting to assign an array to another array. That's not legal in C.

If you want to copy the contents of a character array containing a string to another character array, use strcpy.

strcpy(ptr->name, book_name);
strcpy(ptr->author, author_name);
strcpy(ptr->publisher, publisher_name);
strcpy(ptr->genre, book_genre);

Also note the use of the -> operator to access a pointer-to-member.

Besides this, you're also not reading these 4 string correctly. The %s format specifier to scanf expects a pointer to the first element in a char array. What you're doing is passing in the address of the array itself.

For this, just pass in the name of the array. The array name decays into a pointer to the first element when passed to a function.

scanf("%s",book_name);
printf("Please enter author name:\n");
scanf("%s",author_name);
printf("Please enter publisher name:\n");
scanf("%s",publisher_name);
printf("Please enter book genre:\n");
scanf("%s",book_genre);
Sign up to request clarification or add additional context in comments.

Comments

1

Instead of doing this

(*ptr).name=book_name;

You should copy a string using strcpy like

strcpy((*ptr).name,book_name); 

Also, consider using other safe functions like strncpy().

Note use ptr->name instead of (*ptr).name.

The ptr->name is an array, you cannot change array but you can change contents of an array, which is what the error is suggesting.

1 Comment

Be cautious about strncpy() — it isn't a panacea by any means.

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.