繁体   English   中英

如何更改C数组中的结构?

[英]how can i change the struct in C array?

我在更改作为数组的结构时遇到问题。

我正在使用C语言开发一个图书馆项目,需要将一本书(Book结构)添加到图书馆中,我有一个包含所有书籍的数组,我需要将我的新书添加到该数组中。

我做到了,有人可以帮助我,并给我一点有关此的信息吗?

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

每次编译代码时,都会出现“表达式必须是可修改的左值”的问题。

谢谢

而不是这样做

(*ptr).name=book_name;

您应该使用strcpy复制一个字符串

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

另外,考虑使用其他安全功能,例如strncpy()

请注意使用ptr->name而不是(*ptr).name

ptr->name是一个数组,您不能更改数组,但是可以更改数组的内容,这就是错误提示。

您正在尝试将一个数组分配给另一个数组。 在C语言中这是不合法的。

如果要将包含字符串的字符数组的内容复制到另一个字符数组,请使用strcpy

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

还要注意使用->运算符来访问指向成员的指针。

除此之外,您还无法正确读取这4个字符串。 用于scanf%s格式说明符期望一个指向char数组中第一个元素的指针。 您正在做的是传递数组本身的地址。

为此,只需输入数组的名称。 数组名称在传递给函数时会衰减为指向第一个元素的指针。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM