简体   繁体   中英

What is wrong with my struct typedef causing “dereferencing pointer to incomplete type?”

Im having trouble with my project of the university when i compile my files they are 5 (api.c api.h datastruct.c datastruct.h and main.c) with MakeFile the problem is in the datastruct.c and datastruct.h when compiles this functions:

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

and in the file datastruct.hi have the structure definition:

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;

i cant see the problem datastruct.h is included in datastruct.c!!! The error on compiler is:

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

Read carefully what you wrote:

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

But what is vertex ?

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

So what is a struct vertex_t ? Well, it doesn't exist. You defined the following:

typedef struct _vertex_t {
    ...
} vertex_t;

That's two definitions:

  1. struct _vertex_t
  2. vertex_t

No such thing as struct vertex_t (the reasoning is similar for edge ). Change your typedefs to either:

typedef vertex_t *vertex;
typedef edge_t *edge;

Or:

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

Unrelated to your problem and as said in the comments by user Zan Lynx, allocating with calloc will zero all the members of your struct, therefore initializing them with NULL is superflous.

Your problem is here:

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

it should be:

typedef struct _vertex_t *vertex;
typedef struct _edge_t *alduin;

I found it.

Your problem is in your typedef. In C typedef creates a new type name. Struct names however, are not type names.

So if you change typedef struct vertex_t *vertex into typedef vertex_t *vertex it will fix that error message.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM