我看到其他一些帖子也有同样的问题;然而,其他帖子建议使用 strcpy()。问题是我正在使用 strcpy() 并且仍然收到此错误。如果有人能帮助我解决这个问题,我将非常感激。我将发布我的结构和我遇到问题的代码。
struct movie {
struct movie* next;
struct actor* actors;
char name[100];
int rating;
genre type;
}*list = NULL;
struct actor {
struct actor* next;
char name[100];
};
// Here is the code block i am having troubles with
int add_actor(char* movie_name, char* actor_name)
{
struct movie *temp = list;
struct movie *actor = (struct movie *) malloc(sizeof(struct movie));
while (temp != NULL)
{
if ((strcmp(temp->name, movie_name) == 0))
{
strcpy(list->actors->name, actor_name);
return 1;
}
temp = temp->next;
}
return 0;
}
最佳答案
我猜 struct actor 是一个链表,就像电影一样;如果是的话,这里是代码
int add_actor(char* movie_name, char* actor_name)
{
struct movie *temp = list;
struct actor *actor=NULL;
while (temp != NULL){
if ((strcmp(temp->name, movie_name) == 0)){
actor = calloc(sizeof(struct actor));
strcpy(actor->name, actor_name);
if(temp->actors){
actor->next=temp->actors;
temp->actors=actor;
}else{
temp->actors=actor;
}
return 1;
}
temp = temp->next;
}
return 0;
}
关于c - 嵌套结构,读取字符串 C 的字符时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33193263/