我正在做关于机场模拟的课程,我在尝试将信息存储在角色阵列部分时遇到了一些麻烦.
我应该输入一个字符串,它将存储在planeName节点的一部分,但它似乎无法工作.我int main()现在几乎是空的,因为我不想继续使用不正确的函数进行编码.
以下是我的代码:
struct node {
char planeName[5];
int planeNumber;
struct node* next;
};
struct node* front = NULL;
struct node* rear = NULL;
void Enqueue(char name[5], int x);
int main() {
}
void Enqueue(char name[5], int x){
struct node* temp = (struct node*)malloc(sizeof(struct node));
temp -> planeName = name;
temp -> planeNumber = x;
temp -> next = NULL;
if (front == NULL && rear == NULL)
front = rear = temp;
rear -> next = temp; //set address of rear to address of temp
rear = temp; //set rear to point to temp
return;
}
Run Code Online (Sandbox Code Playgroud)
这是包含以下内容的行中的错误消息:temp -> planeName = name
这是弹出错误消息的部分,我不知道为什么会发生这种情况.
如果我的问题不够明确,有人可以帮忙问我更多问题吗?
temp -> planeName = name;
Run Code Online (Sandbox Code Playgroud)
您无法分配给阵列.数组不能用作左值.strcpy改为使用-
strcpy(temp -> planeName,name);
Run Code Online (Sandbox Code Playgroud)
注意 - 但是确保char在传递数组之前将数组终止为nul strcpy.