This doesn't work for foo
struct Foo;
typedef struct
{
int x;
}
Bar;
void foo (Foo *); // unknown type name ‘Foo’
void bar (Bar *);
typedef struct
{
int y;
}
Foo;
This doesn't work for bar
struct Foo;
typedef struct
{
int x;
}
Bar;
void foo (struct Foo *);
void bar (struct Bar *); ‘struct Bar’ declared inside parameter list
typedef struct
{
int y;
}
Foo;
Some of my structs have to be forward-declared, because they are passed as pointers, and some of them have to be not forward-declared, because they are passes as values.
Is there a way to declare types in C such that all function prototypes can consistently always refer to custom types in the same way, regardless of whether they are forward-declared or not?
Your problem is not forward-declared vs. non-forward-declared, it's
struct Xvs.typedef struct { ... } X.You can solve this by using
struct Xonly:Once you have that, you can introduce
typedefnames:You can't pre-declare
typedefs, so we still need a realstructtype we can forward to. There's a small inconsistency here:Foois the same asstruct Foo, but there is nostruct Bar, onlyBar. You can fix that by switching toThis defines
struct BarandBarsimultaneously.