You break your abstraction if you need to know the definition of struct foo_struct outside of foo.c. The whole point is to make the definition of the "private" structure a specific source file so that the other source files do not know and cannot directly manipulate members of struct foo_struct .
You need to either move bar to foo.c, or you need to put the structure definition in foo.h (making it publicly available), or you need to define an interface that allows other translation units to highlight, set, read and free elements of type foo_s without exposing details of its type, similar to how routines in stdio.h manage objects of type FILE , something like
foo_s *create_foo(int a, int b); void set_foo(foo_s *f, char *property, int value); int get_foo(foo_s *f, char *property); void destroy_foo(foo_s **f);
You would add the above interface to foo.h and implement it in foo.c; functions in other translation units (source files) will use it as:
void blah(void) { foo_s *f = create_foo(0,0); if (f) { set_foo(f, "a", 1); set_foo(f, "b", 2); printf("a = %d, b = %d\n", get_foo(f, "a"), get_foo(f, "b"); destroy_foo(&f); assert(f == NULL); } }
There are probably a hundred best ways to do this, but you should get this idea.
source share