gpt4 book ai didi

c - 将新字符串值分配给 char 数组时出错

转载 作者:行者123 更新时间:2023-11-30 20:09:04 25 4
gpt4 key购买 nike

当尝试将新的字符串值分配给 c 中现有的 char 数组时,出现以下错误:

assignment to expression with array type
employees[id].FirstMiddleName = NewFirstMiddleName;
^

我认为这两个变量都是相同大小的数组,所以我不明白错误指的是什么或如何修复它。

struct employee {
char LastName[30];
char FirstMiddleName[35];
float Salary;
int YearHired;
};

int modify(int id) {
char NewLastName[30];
char NewFirstMiddleName[35];
float NewSalary;
int NewYearHired;

printf("Enter new first (and middle) name(s): \n");
gets(NewFirstMiddleName);
employees[id].FirstMiddleName = NewFirstMiddleName;

printf("Enter new last name: \n");
gets(NewLastName);
employees[id].LastName = NewLastName;
....

}

int main() {

struct employee *ptr, person;
ptr = &person;

ptr->LastName[0] = '\0';
ptr->FirstMiddleName[0] = '\0';
ptr->Salary = -1;
ptr->YearHired = -1;

for(int i = 0; i < 20; i++) {
employees[i] = person;
//printf("%i\n", i);
}
....
}

最佳答案

employees[id].FirstMiddleName 是数组类型,不能使用 =

赋值

assignment operator shall have a modifiable lvalue as its left operand. A modifiable lvalue is an lvalue that does not have array type

在这种情况下,您需要使用strcpy

strcpy(employees[id].FirstMiddleName, NewFirstMiddleName);

关于c - 将新字符串值分配给 char 数组时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53975030/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com