2016-10-03 55 views
0

我试图创建一个struct typedef movie_t并将其添加到列表中。它包含标题,导演,评分和制作年份。我的代码只是输出标题。我不明白为什么它不输出movie_t结构的所有成员?将结构添加到列表并获取正确的输出

例如,我已经定义:

movie_t *first_movie = {"The Outsiders","Francis Ford Coppola",'PG13',1983} 

但是当我运行我的程序只输出标题:

list[0] = The Outsiders 
list[1] = The Outsiders 
list[2] = The Outsiders 
list[3] = The Outsiders 
list[4] = The Outsiders 
list[5] = The Outsiders 
list[6] = The Outsiders 
list[7] = The Outsiders 
list[8] = The Outsiders 
list[9] = The Outsiders 

basiclist.h

#ifndef BASICLIST_H_ 
#define BASICLIST_H_ 

typedef struct node { 
    void * data;   /* pointer to data */ 
    struct node * next; /* pointer to next next node */ 
} node_t; 

int list_add(node_t ** list, void * data); 

#endif 

movie.h

#ifndef MOVIE_H 
#define MOVIE_H 
#define SIZE_LIMIT 25 
#define RATING_SIZE 5 

typedef enum {G, PG, PG13, R} rating_t; 

typedef struct { 
    char rating; 
    char title[SIZE_LIMIT]; 
    char director[SIZE_LIMIT]; 
    int year; 
}movie_t; 


void get_movie(movie_t * movie); 
void print_movie(const movie_t * movie); 

#endif /* MOVIE_H */ 

的main.c

#include "movie.h" 
#include <stdlib.h> 
#include <stdio.h> 
#include "basiclist.h" 

int list_add(node_t ** list, void * data) { 
    int ret = 0; 
    node_t * newnode = (node_t *) malloc(sizeof(node_t)); 
    if (newnode == NULL) { 
    ret = -1; 
    } 
    else { 
    newnode->data = data; 
    newnode->next = *list; 
    } 
    *list = newnode; 
    return ret; 
} 

int main (void) 
{ 
    int ii; 
    movie_t * new_movie; 
    movie_t *first_movie = {"The Outsiders","Francis Ford Coppola",'PG13',1983}; 
    node_t * list = NULL; 
    node_t * curr; 

    for(ii=0;ii<10;ii++) { 
    new_movie = (movie_t *) malloc(sizeof(int)); 
    *new_movie = *first_movie; 
    list_add(&list, new_movie); 
    } 

    ii = 0; 
    curr = list; 
    while (curr != NULL) { 
    printf("list[%d] = %s\n", ii, *((movie_t *) curr->data)); 
    ii++; 
    curr = curr->next; 
    } 
    printf("It worked!\n"); 
    return 0; 
} 
+0

你明白如何'的malloc()'的作品?你为什么'malloc()'两次? –

+1

首先,''PG13' - 为什么一个多字符字符突然不变?其次,你的'printf'是故意制作的,只输出一个字符串 - 它只有一个'%s'说明符。阅读'printf'上的文档。 – AnT

+0

我是C新手,我想这是我的问题。如何修改它以便输出标题,目录,评分,年份?我尝试阅读并改变它。如果我不再将'first_movie'作为指针,它会输出导演而不是标题。我没有考虑评分。我已经将它更改为char []谢谢指出。如果我从note_t中移除malloc(),则会收到编译错误,如果我从new_movie中将它移除,则会收到段错误。 – mdo123

回答

0

printf("list[%d] = %s\n", ii, *((movie_t *) curr->data));movie_tmovie_t*不适合%s。您为move_t*制作自定义打印功能(print_movie)。

一些诸如

void print_movie(FILE* fp, const movie_t *m){ 
    static const char *raiting[] = { "G", "PG", "PG13", "R"}; 
    fprintf(fp, "%s\t%s\t%s\t%d\n", m->title, m->director, raiting[m->rating], m->year); 
} 

变化原型void print_movie(const movie_t * movie);void print_movie(FILE* fp, const movie_t *m);
或更改名称,并从print_movie调用。

在主

变化printf("list[%d] = %s\n", ii, *((movie_t *) curr->data));
print_movie(stdout, curr->data);


另外
变化movie_t *first_movie = {"The Outsiders","Francis Ford Coppola",'PG13',1983};
movie_t first_movie = {"The Outsiders","Francis Ford Coppola", (char)PG13, 1983};

变化

typedef struct { 
    char rating; 
    char title[SIZE_LIMIT]; 
    char director[SIZE_LIMIT]; 
    int year; 
}movie_t; 

typedef struct { 
    char title[SIZE_LIMIT]; 
    char director[SIZE_LIMIT]; 
    char rating;//or rating_t rating; 
    int year; 
}movie_t; 

​​

for(ii=0;ii<10;ii++) { 
    new_movie = malloc(sizeof(*new_movie)); 
    *new_movie = first_movie; 
    list_add(&list, new_movie); 
} 
+0

谢谢你的工作。但我有两个问题。 1.当我在Windows上运行我的原始代码时,我收到了段错误,但是我得到了粘贴在Ubuntu上的输出。为什么这种差异? 2.我是C新手,我不太了解使用'FILE * fp',你能否澄清一下? – mdo123

+0

@ mdo123 1)这真是幸运。未定义的行为是未定义的行为。 2)如果你需要输出到文件。 – BLUEPIXY

相关问题