#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int id;
int salary;
struct node *next;
char name[30];
} List;
int main() {
List *head = (List *) malloc(sizeof(List)); //allocated memory for head
if(head == NULL) {
return 1;
}
head -> id = 332513075;
head -> salary = 100;
head -> name = "Name Lastname";
// head -> phone = "0532554891";
// head -> position = "CEO";
head -> next = NULL;
print_list(*head);
return 0;
}
Error message: array type 'char [30]' is not assignable What is going wrong here and how to fix it?
Arrays are non-modifiable lvalues. That is arrays do not have the assignment operator. You have to copy elements from one array into another.
So instead of this statement
head -> name = "Name Lastname";
write
#include <string.h>
//...
strcpy( head -> name, "Name Lastname" );
本文解析了如何在C语言中正确地为结构体内的字符数组赋值,避免因数组非可修改性导致的错误。通过实例演示了使用strcpy函数代替直接赋值,并解释了原因。

被折叠的 条评论
为什么被折叠?



