You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言报错:Expression must be a modifiable lvalue(修改结构体成员时)新手求助

Hey there! Let's figure out why you're hitting that "Expression must be a modifiable lvalue" error and get your student registration code working right.

First, let's break down the root issue

Your StudentList is an array of pointers to struct Student, but when you first declare it, all those pointers are uninitialized—they point to random, invalid memory (wild pointers). Trying to modify struct members through an uninitialized pointer is undefined behavior, and this can trigger the lvalue error you're seeing.

But the bigger problem is how you're passing the array element to your Register function. C uses pass-by-value, so if you pass StudentList[0] (a struct Student*) directly, you're only sending a copy of that pointer. Any changes you make to the pointer itself inside the function won't affect the original array element. If you're trying to assign a new struct Student instance to StudentList[0], this approach won't work—and that's likely the main cause of your error.

Here's how to fix it

To modify the original pointer in your array, you need to pass a pointer to a pointer (struct Student**) to your Register function. This lets the function directly access and update the pointer in your StudentList array.

Step 1: Rewrite your Register function

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

// Your struct definition
struct Student {
    char FirstName[20];
    char LastName[20];
    char StudentID[10];
    char Password[20];
};

void Register(struct Student **studentPtr) {
    // Allocate memory for a new Student struct
    *studentPtr = malloc(sizeof(struct Student));
    if (*studentPtr == NULL) {
        printf("Oops, failed to allocate memory!\n");
        return;
    }

    // Now safely modify the struct members
    strcpy((*studentPtr)->FirstName, "Alice");
    strcpy((*studentPtr)->LastName, "Smith");
    strcpy((*studentPtr)->StudentID, "987654321");
    strcpy((*studentPtr)->Password, "securePass123");
}

Step 2: Call the function correctly

When you call Register, pass the address of StudentList[0] (not the pointer itself):

int main() {
    struct Student *StudentList[10];

    // Pass the address of the first pointer in the array
    Register(&StudentList[0]);

    // Test that it worked
    printf("First Name: %s\n", StudentList[0]->FirstName);
    printf("Student ID: %s\n", StudentList[0]->StudentID);

    // Don't forget to free the allocated memory when done!
    free(StudentList[0]);
    return 0;
}

What if you just need to modify an existing struct?

If StudentList[0] already points to a valid, allocated struct Student (you've already assigned memory to it), you don't need a double pointer. Just pass the pointer directly:

void UpdateStudent(struct Student *student) {
    strcpy(student->FirstName, "Bob");
    strcpy(student->LastName, "Jones");
}

// Usage in main:
StudentList[0] = malloc(sizeof(struct Student));
UpdateStudent(StudentList[0]);

This works because you're modifying the data that the pointer points to, not the pointer itself—so the pass-by-value copy of the pointer is still valid for accessing the original struct.

Quick reminders

  • Always check if malloc returns NULL—it can fail if your system runs out of memory.
  • Don't forget to free any memory you allocate with malloc to avoid memory leaks.
  • When using strcpy, make sure your strings don't exceed the size of the char arrays in your struct (e.g., FirstName[20] can hold up to 19 characters plus the null terminator).

内容的提问来源于stack exchange,提问作者Val

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:03:00