【问题标题】:Nested Structure,Error Reading Characters of String C嵌套结构,错误读取字符串 C 的字符
【发布时间】:2016-01-16 13:14:09
【问题描述】:

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

}

【问题讨论】:

  • 仍然缺少结构演员
  • 对不起,我一定错过了这里生病编辑帖子
  • @milevyo 好吧,那里的结构正确

标签: c linked-list structure strcpy


【解决方案1】:

我猜 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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-28
    • 1970-01-01
    • 2015-04-07
    • 1970-01-01
    • 1970-01-01
    • 2018-07-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多