c - 我的 struct typedef 有什么问题导致 "dereferencing pointer to incomplete type?"

标签 c pointers gcc data-structures dereference

当我用 MakeFile 编译我的文件时,我的大学项目遇到了问题,它们是 5(api.c api.h datastruct.c datastruct.h 和 main.c),问题出在 datastruct.c 和 datastruct .h 编译这个函数时:

vertex new_vertex() {
    /*This functions allocate memorie for the new struct vertex wich save 
    the value of the vertex X from the edge, caller should free this memorie*/

    vertex new_vertex = NULL;

    new_vertex = calloc(1, sizeof(vertex_t));
    new_vertex->back = NULL;
    new_vertex->forw = NULL;
    new_vertex->nextvert = NULL;

    return(new_vertex);   
}

在文件 datastruct.h 中我有结构定义:

typedef struct vertex_t *vertex;
typedef struct edge_t *alduin;

typedef struct _edge_t{
    vertex vecino;      //Puntero al vertice que forma el lado
    u64 capacidad;      //Capacidad del lado
    u64 flujo;          //Flujo del lado       
    alduin nextald;          //Puntero al siguiente lado
}edge_t;

typedef struct _vertex_t{
    u64 verx;   //first vertex of the edge
    alduin back; //Edges stored backwawrd
    alduin forw; //Edges stored forward
    vertex nextvert;

}vertex_t;

我看不到问题 datastruct.h 包含在 datastruct.c 中!!! 编译器的错误是:

gcc -Wall -Werror -Wextra -std=c99   -c -o datastruct.o datastruct.c
datastruct.c: In function ‘new_vertex’:
datastruct.c:10:15: error: dereferencing pointer to incomplete type
datastruct.c:11:15: error: dereferencing pointer to incomplete type
datastruct.c:12:15: error: dereferencing pointer to incomplete type

最佳答案

仔细阅读你写的内容:

vertex new_vertex = NULL; // Declare an element of type 'vertex'

但是顶点是什么?

typedef struct vertex_t *vertex; // A pointer to a 'struct vertex_t'

那么什么是struct vertex_t?好吧,它不存在。您定义了以下内容:

typedef struct _vertex_t {
    ...
} vertex_t;

这是两个定义:

  1. 结构_vertex_t
  2. vertex_t

没有 struct vertex_t 这样的东西(edge 的道理类似)。将您的 typedef 更改为:

typedef vertex_t *vertex;
typedef edge_t *edge;

或者:

typedef struct _vertex_t *vertex;
typedef struct _edge_t *edge;

与您的问题无关,正如用户 Zan Lynx 在评论中所说,使用 calloc 分配会将结构的所有成员归零,因此使用 NULL 初始化它们是多余的。

关于c - 我的 struct typedef 有什么问题导致 "dereferencing pointer to incomplete type?",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23796189/

相关文章:

c - 从二进制文件更新记录(项目的一部分)(段错误)

c++ - 二维数组重新分配

c++ - vector 指针位置保证?

c++ - 错误在 `./2' : free(): invalid pointer: 0x000000000096044c *** Aborted (core dumped)

C 多结构体只有一个节点

c++ - 使用 MPI 进行并行编程以使用动态二维数组进行矩阵乘法时如何解决问题?

c++ - 无法识别的命令行选项 '-stdlib=libc++' gcc (Homebrew gcc 5.3.0) 5.3.0

c++ - 在预处理指令后避免对(生成的) token 发出 gcc 警告?

c - 使用__libc_init_array调用STM32

c - sighandler 中的第二个信号调用 - 为什么?