c - 嵌套结构,读取字符串 C 的字符时出错

标签 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;

}

最佳答案

我猜 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/

相关文章:

c - 字段的 __attribute__((packed)) 如何影响包含该字段的结构?

java - 我想通过递归算法在链表中找到最大节点,但我的代码有问题

c - 附加到链表时出现段错误

c - 带链表的多线程

c - 如何确定C中结构体的大小

mysql - 在 MySQL 中保存用户首选项的最有效方法是什么?

c - amqp_login 给出 "Argument list too long"错误?

java - 将位修改代码从 C 移植到 Java

c# - pinvoke 在调用 C 代码时给出 AccessViolationException

c - C中指向结构和数组的指针