6
4

3 回答 3

6

The answer is that there is one, and you should use an header file instead. You can copy the definition of the struct typedef struct BAR_{...} bar; into test_foo.c and it will work. But this causes duplication. Every solution that works must make the implementation of struct available to the compiler in test_foo.c. You may also use an ADT if this suits you in this case.

于 2010-07-12T10:35:43.347 回答
4

Drop the typedef.

In foo.c:

struct bar 
{
    ...
};

struct bar *bar_new(....)
{
    return malloc(sizeof(struct bar));
}

In test_foo.c:

struct bar;

struct bar *mybar = bar_new(...);

Note that you only get the existence of a struct bar object in this way, the user in test_foo.c does not know anything about the contents of the object.

于 2010-07-12T10:39:51.097 回答
1

You would need to supply the definition of BAR in test_foo.c. Whether that duplication is preferable to having a header is up to you.

于 2010-07-12T10:33:17.683 回答