c - : '=' : left operand must be l-value? 是什么意思

标签 c struct

我正在使用结构编写代码,该结构将读取包含书籍数据的文本文件。姓名、作者、出版商、流派等...

我的头文件如下所示:

typedef struct book{    

    char name[NAME_LENGTH];
    char authors[AUTHORS_NAME_LENGTH];
    char publisher[PUBLISHER_NAME_LENGTH];
    char genre[GENRE_LENGTH];
    int year;
    int num_pages;
    int copies;
}book;


typedef struct library
{    

    book books[BOOK_NUM];

}library;

typedef char* string;

代码是:

    if (NULL == (incoming_books = fopen(".\books.txt", "r")))
    {            /* opening file for reading */
        /*printf("Error opening file"); write to file*/
        exit(1);
    }
    while (!feof(incoming_books))
    {
        fgets(line,200,incoming_books);//copies one line, assuming no longer than 200 chars

        idx_helper = strchr(line, '$');//finds '$' index, attribures are seperated by "$$$"
        index = (int)(idx_helper - line);// cast index into int
        char_num = index;
        if (NULL != memcpy(temp_string, line, char_num))//copies string (name)
            temp_book->name = *temp_string;

        index += 3;                         // incrementing index by 3
        idx_helper = strchr(&line[index], '$');   // same for authors
        index = (int)(idx_helper - &line[index]);
        char_num = index;
        if (NULL != memcpy(temp_string, &line[index], char_num*sizeof(char)))
        temp_book->authors = *temp_string;
    }

每个图书属性依此类推

我收到两个错误: 1. 错误 7 错误 C2106: '=' : 左操作数必须是左值 它指向行 temp_book->name = *temp_string;和 temp_book->authors = *temp_string; 2. IntelliSense:表达式必须是可修改的左值 它指向相同的线。

会不会是指针问题?

最佳答案

因为数组是不可修改的左值

摘自C11 Standard Draft N1570 —(此答案中突出显示了粗体部分,以帮助读者找到有趣的部分)。

6.3.2.1 Lvalues, arrays, and function designators

  1. An lvalue is an expression (with an object type other than void) that potentially designates an object; 64) if an lvalue does not designate an object when it is evaluated, the behavior is undefined. When an object is said to have a particular type, the type is specified by the lvalue used to designate the object. A modifiable lvalue is an lvalue that does not have array type, does not have an incomplete type, does not have a const-qualified type, and if it is a structure or union, does not have any member (including, recursively, any member or element of all contained aggregates or unions) with a const-qualified type.

64)The name ‘‘lvalue’’ comes originally from the assignment expression E1 = E2, in which the left operand E1 is required to be a (modifiable) lvalue. It is perhaps better considered as representing an object ‘‘locator value’’. What is sometimes called ‘‘rvalue’’ is in this International Standard described as the ‘‘value of an expression’’.

您使用memcpy()复制到temp_string,您应该使用它直接复制到数组。另外,不要使用 sizeof(char),因为它永远不会是 1 之外的任何内容,并且始终为 nul 终止符分配空间,然后复制nul 终止符。否则,当您将数组传递给需要 字符串 的函数时,例如各种 str* 函数或带有 printf() ” %s" 说明符未定义的行为将发生。

此外,在您的代码中有这个

while (!feof(incoming_books))

只有当 fgets() 失败时,这才会是true,那么它会在第一次 fgets() 失败后发生,并且当您永远不要检查最后一行是否会重复,你应该这样做

while (fgets(line, 200, incoming_books) != NULL)

关于c - : '=' : left operand must be l-value? 是什么意思,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34707110/

相关文章:

c - 命令行参数和结构体参数

c - 如何在 Windows 上将千位分隔符添加到 C 中的 double ?

C:传递数组代替变量参数列表

ios - 由于单元格为空,如何检索 UserDefaults

c++ - 结构数组和新建/删除

go - 如何使函数的参数是 "generic"结构

c - 离散余弦变换DCT实现C

c - 使用 toupper 将字符数组传递给函数

c++ - 在cpp函数中初始化多个结构

c++ - C++中结构/类的历史是什么?