首页 > 解决方案 > 学生单链表和错误“左操作数必须是左值”。怎么修?

问题描述

我需要创建一个程序,其中将创建一个简单的学生单链表。我需要让更改给定学生的名字成为可能。编译时出现错误:

Severity    Code    Description Project File    Line    Suppression State
Error (active)  E0137   expression must be a modifiable lvalue  Student Work    C:\Users\John\source\repos\Student Work\Student Work\main.c 87  
Error (active)  E1072   a declaration cannot have a label   Student Work    C:\Users\John\source\repos\Student Work\Student Work\main.c 111 
Error   C2106   '=': left operand must be l-value   Student Work    C:\Users\John\source\repos\Student Work\Student Work\main.c 87  

我该如何解决这些错误?此外,建议我如何改进我的代码?也许有些东西可以写得更容易、更好或更有效?main.c 代码:

#include <stdio.h>
#include <string.h>
#include <windows.h>
#define MAX 3
#define KOL 15
 
struct Student
{
    int id;
    char Name[KOL];
    int Age;
    float AverageRaiting;
    struct Student* nextStudent;
};
 
void InitStudentList(struct Student** student)
{
    *student = (struct Student*)
        malloc(sizeof(struct Student));
    (*student)->id = 1;
    printf("Enter 1 student name: ");
    scanf("%s", (*student)->Name);
    printf("Enter 1 student age: ");
    scanf("%d", &(*student)->Age);
    printf("Enter 1 student average raiting: ");
    scanf("%f", &(*student)->AverageRaiting);
    printf("\n");
    (*student)->nextStudent = NULL;
    struct Student* endStudent = *student;
    for (int i = 2; i <= MAX; i++)
    {
        endStudent->nextStudent =
            (struct Student*) malloc(sizeof(struct Student));
        endStudent = endStudent->nextStudent;
        endStudent->id = i;
        printf("enter %d student name: ", i);
        scanf("%s", endStudent->Name);
        printf("Enter %d student age: ", i);
        scanf("%d", &endStudent->Age);
        printf("Enter %d student average raiting: ", i);
        scanf("%f", &endStudent->AverageRaiting);
        printf("\n");
        endStudent->nextStudent = NULL;
    }
}
 
void PrintList(struct Student* student)
{
    struct Student* printStudent = student;
    printf("==========================\n");
    printf("Number         Name         Age  average raiting \n");
    printf("==========================\n");
    while (printStudent)
    {
        printf("%d", printStudent->id);
        printf("%-15s", printStudent->Name);
        printf("%4d", printStudent->Age);
        printf("%8.2f", printStudent->AverageRaiting);
        printf("\n");
        printStudent = printStudent->nextStudent;
    }
    printf("==========================\n");
}
 
void FreeList(struct Student** student)
{
    if (*student == NULL)
        return;
    struct Student* tmp = *student;
    struct Student* curr_stud;
    while (tmp)
    {
        curr_stud = tmp;
        tmp = tmp->nextStudent;
        free(curr_stud);
    }
    *student = NULL;
}
 
void ChangeStudentName(int n, char name[KOL], struct Student* student)
{
    struct Student* changingStudent = student;
    while (changingStudent)
    {
        if (changingStudent->id == n)
        {
            changingStudent->Name = name;
            printf("Changes are saved");
            break;
        }
    }
}
 
int main(void)
{
    int command;
    struct Student* BaseStudent = NULL;
    SetConsoleCP(1251);
    SetConsoleOutputCP(1251);
    InitStudentList(&BaseStudent);
    for (;;)
    {
        printf("Enter command:\n 1 - Show student list,\n 2 - change student name,\n 3 - exit\n");
        scanf("%d", &command);
        switch (command)
        {
        case 1:
            PrintList(BaseStudent);
            break;
        case 2:
            int n;
            char name[KOL];
            printf("Enter student number: ");
            scanf("%d", &n);
            printf("Enter new student name: ");
            scanf("%s", &name);
                ChangeStudentName(n, name, BaseStudent);
            break;
        case 3:
            FreeList(&BaseStudent);
            return 0;
            break;
        default:
            printf("Error...");
            FreeList(&BaseStudent);
            return 0;
            break;
        }
    }
    return 0;
}

提前致谢!

标签: c

解决方案


您的代码中有两个问题:

  1. 在您的ChangeStudentName()方法中,您不能直接将一个数组复制到另一个数组。您可以使用方法将一个字符数组复制到另一个字符数组strcpy()

    例如-strcpy(changingStudent->Name,name);

  2. 在您的 main() 方法中,

    case 2:,你不应该像这样在 switch 语句中声明变量。

查看此链接了解更多信息:为什么不能在 switch 语句中声明变量?

如果要在 switch 语句中声明变量,则应使用大括号,如下所示。

例如-

case 2:{
            int n;
            char name[KOL];
            printf("Enter student number: ");
            scanf("%d", &n);
            printf("Enter new student name: ");
            scanf("%s", &name);
                ChangeStudentName(n, name, BaseStudent);
            break;
       }

希望能帮助到你!!


推荐阅读