嵌套结构,错误读取字符串C的字符

时间:2015-10-18 01:02:51

标签: c linked-list structure strcpy

我看到其他一些帖子也有同样的问题;但是其他帖子建议使用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;

}

1 个答案:

答案 0 :(得分: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;
}