malajedala malajedala - 1 year ago 70
C Question

"dereferencing pointer to incomplete type” in a struct pointer

So I have defined the following structure in my header


typedef struct path Path;

struct Path {

Path* branching_paths;
uint32_t nb_paths;

and in the same header I have declared and defined the following inline function:

inline void init_path(Path* path);

inline void init_path(Path* path){

path->branching_paths = NULL;
path->nb_paths = 0;

And I don't know for what reason I am getting this error.. I already searched the web but as far as I see i have defined the structure properly in the header, or am I missing something?

./../Path.h:54:9: error: dereferencing pointer to incomplete type 'Path {aka struct path}'
path->branching_paths = NULL;

Answer Source

There is a typo in your typedef.

You define a typedef for struct path, then you later define struct Path. Because C is case sensitive, these are seen as two separate types.

The way you have it now, you declared struct path (with Path as an alias) but haven't defined it. So when you try to dereference a Path *, the compiler doesn't know what that struct looks like because you haven't told it.

What you want is:

typedef struct Path Path;