Question

I have this in input_lib.c:

#include "input_lib.h"

struct edge {
    int from;
    int to;
};

input_lib.h contains my typedef:

typedef struct edge edge;

Then in another file which includes input_lib.h, I declare a variable and attempt to use it:

edge *my_edge = read_next_edge();
printf("%d, %d", my_edge->from, my_edge->to);

I get the error at the printf line:

error: dereferencing pointer to incomplete type

read_next_edge() returns an edge* that is allocated by:

edge *next_edge = malloc(sizeof(struct edge));

My C is a little rusty so I assume I've just made a small mistake somewhere, but I'm having a really hard time figuring out where. I would really appreciate if someone could explain where my mistake is, and why I'm getting it. Thanks

Was it helpful?

Solution 2

If you are expecting to access the internals of the structure outside of input_lib.c, then the structure definition needs to be in input_lib.h. If you are expecting code outside of input_lib.c to use access functions to get at the information, then you can leave the structure opaque (incomplete) but you need to call the functions to get the information.

Either way works; choose one and stick with it.

OTHER TIPS

Your struct definition should be in input_lib.h

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top