c - 将结构添加到列表并获得正确的输出

标签 c list struct typedef

我正在尝试创建一个 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

电影.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 */

ma​​in.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;
}

最佳答案

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);
}

将原型(prototype) 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 = {"局外人","弗朗西斯·福特·科波拉",'PG13',1983};
movie_t first_movie = {"局外人","弗朗西斯·福特·科波拉", (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 = (movie_t *) malloc(sizeof(int));//int !!
  *new_movie = *first_movie;
  list_add(&list, new_movie);
}

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

关于c - 将结构添加到列表并获得正确的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39823624/

相关文章:

c - 在字符串中的 "E"之后插入 "T",不使用 "T"回火

c - 有效地从 CSV 中读取特定行,C

c - 我需要 C 程序方面的帮助

python - 使用列表检查按钮状态(tkinter python)

c++ vector/类/结构到简短的冗长代码

c - 具有整数参数的函数

c# - 对包含新添加的自定义类型的列表进行排序

python - 如何随机播放存储在 Python 文件中的非常大的列表?

使用链表的 malloc() 和 free 的正确方法

c - 取消引用指向不完整类型的指针|