【问题标题】:Warning: Incompatible pointer types assigning to 'node_t *' (aka 'struct node *') from 'mode_t' (aka 'unsigned int *')警告:从“mode_t”(又名“unsigned int *”)分配给“node_t *”(又名“struct node *”)的指针类型不兼容
【发布时间】:2019-04-28 03:45:50
【问题描述】:

我们的项目是关于使用我们学院的一位教授开发的机器人和软件制作电影。该库称为 MyroC,机器人利用该库执行许多功能。理想情况下,我们让机器人拍摄用户输入的许多帧(图片)。用户还可以选择在主电影中插入“场景”或小子电影。

我是一名学习 C 语言的初学者。在我最近的学校项目中,我和我的搭档在 C 语言中使用指针和链表时遇到了很多麻烦。我们了解为列表中的节点分配值和更改地址的基本思想,但我们不能似乎弄清楚我们的警告来自哪里。我们搜索了各种在线资源和文本来比较我们的代码语法,我们没有发现我们可能做错了什么。

这是我们的代码:

//This part is contained in a separate header file called "movie.h"

    struct node { /* Singly-linked list nodes contain a Picture and point to next */
      Picture frame;
      node_t * next;
    };

    typedef struct node node_t;   /* Shorthand type for nodes in the picture list */

    typedef struct {     /* Wrapper struct for the movie as a linked list */
      node_t * first;
      node_t * last;
    } movie_t;

// This part is contained in a separate file called "movie.c" 
// An appropriate reference is made to movie.h
// #include "movie.h"

    movie_t
    create (void)
    {
      movie_t movie = {NULL, NULL}; // initially create an empty movie
      return movie;
    } // movie

    size_t
    size (movie_t * movie)
    {
      unsigned int count = 0;
      node_t * current = movie->first;
      while (current != NULL) {
        count++;
        current = current->next;
      }
        return count;
    } // size

    bool
    is_empty (movie_t * movie)
    {
      if (size(movie)==0) // movie contains no frames
        return true;
      else
        return false; // movie contains frames
    } // empty


    bool
    add (movie_t * movie, Picture frame) // add a frame to the end of the movie
    {
      int before_size = size(movie);

      node_t * new_node;
      new_node = malloc(sizeof(node_t));

      if (new_node == NULL) {
        printf("Error, malloc failed.\n");
        exit(EXIT_FAILURE);
      }

      node_t * cursor = movie->first;
      while(cursor->next != NULL) {
        cursor=cursor->next;
      }
      cursor->next = new_node;
      movie->last = new_node;
      new_node->frame = frame;

      if (before_size < size(movie) && (is_empty(movie)==false))
        return true;
      else
        return false;
    } // add


    void // insert a frame before index
    insert (movie_t * movie, movie_t * scene, unsigned int index)
    {
      node_t *insertion;
      insertion = malloc(sizeof(node_t));
      if (insertion == NULL) {
        printf("Error, malloc failed.\n");
        exit(EXIT_FAILURE);
      }

      insertion = movie->first;

      for (unsigned int i = 0; i < index; i++) {
        if (insertion != NULL) 
          insertion = insertion->next;
      }
      scene->last = insertion->next;
      insertion->next = scene->first;

    } // insert

终端输出

error: unknown type name 'node_t'; did you mean 'mode_t'?
node_t * next;
^~~~~~
mode_t   
/usr/include/x86_64-linux-gnu/sys/types.h:70:18: note: 'mode_t' declared here    

【问题讨论】:

  • 关于:movie_t create (void) { movie_t movie = {NULL, NULL}; // initially create an empty movie return movie; } 不可能返回整个结构。因此,强烈建议使用calloc()(或类似的)将movie_t 的实例放入堆中,让函数返回指向该实例的指针。
  • 除其他问题外,发布的代码无法编译!它无法编译的一个原因是它缺少所需头文件所需的 #include 语句。
  • 请发minimal reproducible example,以便我们重现问题并帮助您调试
  • 关于:struct node { Picture frame; node_t * next; }; 在结构中,总是拼出全名。所以node_t * next; 应该是:struct node * next;
  • 发布的代码与编译器的错误/警告消息不匹配!请通过发布您实际使用的代码的minimal reproducible example 来更正此问题。

标签: c pointers linked-list malloc movie


【解决方案1】:

您的第一条错误消息揭示了一切。您在声明它之前使用 node_t ,您在它下面执行它。

事物需要在使用前声明/定义。

所以动起来……

typedef struct node node_t;   /* Shorthand type for nodes in the picture list */

所以变成了……

typedef struct node node_t;   /* Shorthand type for nodes in the picture list */

//This part is contained in the header file "movie.h"
struct node { /* Singly-linked list nodes contain a Picture and point to next */
   Picture frame;
    node_t * next;
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-08
    • 1970-01-01
    • 2018-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多