gpt4 book ai didi

arrays - 修复char格式错误——Linux系统C程序

转载 作者:行者123 更新时间:2023-12-04 07:53:07 24 4
gpt4 key购买 nike

我试图摆脱我的程序中的错误。我不确定数组有什么问题。
这是示例代码:

typedef struct listNoNodes
{
char FirstName[50];
char LastName[60];
int ID;
int age;
struct listNoNodes* next;
} listNoNodes;


listNoNodes* listNodes(char FirstName[], char LastName[], int ID, int age,
listNoNodes* next)
{
listNoNodes* tmp = (listNoNodes*)malloc(sizeof(listNoNodes));
tmp->FirstName[50] = FirstName;
tmp->LastName[60] = LastName;
tmp->ID = ID;
tmp->age = age;
tmp->next = next;
return tmp;
};
这是错误
lab3.c: In function ‘listNodes’:
lab3.c:21:21: warning: assignment to ‘char’ from ‘char *’ makes integer from pointer without a cast [-Wint-conversion]
21 | tmp->FirstName[50] = FirstName;
| ^
lab3.c:22:20: warning: assignment to ‘char’ from ‘char *’ makes integer from pointer without a cast [-Wint-conversion]
22 | tmp->LastName[60] = LastName;

最佳答案

这两行是错误的:

tmp->FirstName[50] = FirstName;
tmp->LastName[60] = LastName;
您正在尝试分配 char*指向单个 char 的指针元素。
您需要复制数组的内容,例如:
strncpy(tmp->FirstName, FirstName, 50);
strncpy(tmp->LastName, LastName, 60);

关于arrays - 修复char格式错误——Linux系统C程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66849918/

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