When I'm trying to use and access the pointers to my structs i keep getting the annoying message of "dereferencing pointer to incomplete type"
....
For example in my user.h
file I have this typedef
:
typedef struct FacebookUser_t* User;
and in my user.c
file which includes user.h
I have this struct:
struct FacebookUser_t {...};
So when I need a pointer to that struct I only use User blabla;
it seems to work, and I add it to generic list as Element
which is void*
and that's the typedef for it in list.h
:
typedef void* Element;
and when I get back a node from the list which contains Element
(User
) I can't access it's members, what I'm doing wrong? Thanks!
If you want to hide the definition of a structure (by sticking the actual
struct {
block in a single C file and only exposing atypedef
ed name in the header, you cannot expect to access the fields directly.One way around this is to continue with the encapsulation, and define accessor functions, i.e. you'd have (in
user.h
):Please note that including the
*
in thetypedef
is often confusing, in my opinion.The problem is that the C file doesn't have access to the implementation of that strucure.
Try to move the definition of the structure in the header file.
As I understand it you try to access the members of User...
...via a Element, which is a void *. This can not work, because the compiler does not know what type it should dereference. For example:
You first need to tell the compiler what your void * is pointing to. To do that you cast the pointer:
I hope I understood your problem and this helps :)