accessing struct: derefrencing pointer to incomplete type

后端 未结 3 949
盖世英雄少女心
盖世英雄少女心 2021-01-27 08:58

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 e

3条回答
  •  [愿得一人]
    2021-01-27 09:25

    If you want to hide the definition of a structure (by sticking the actual struct { block in a single C file and only exposing a typedefed 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):

    const char * user_get_name(const User user);
    void         user_set_name(User user, const char *new_name);
    ...
    

    Please note that including the * in the typedef is often confusing, in my opinion.

提交回复
热议问题