How do I forward declare FILE *
in C? I normally do this using struct MyType;
, but naturally this doesn't appear to be possible.
If behaviour differs between C standards or compilers and with C++, this is also of interest.
Why I want to do this aside: What I'm asking is how to forward declare a non-struct/"typedef'd struct" type so that I can declare pointers to it. Obviously using void *
and casting it in the source file is a bit hackish.
A forward declaration tells the compiler about the existence of an entity before actually defining the entity. Forward declarations can also be used with other entity in C++, such as functions, variables and user-defined types.
A forward declaration allows us to tell the compiler about the existence of an identifier before actually defining the identifier. In the case of functions, this allows us to tell the compiler about the existence of a function before we define the function's body.
In Objective-C, classes and protocols can be forward-declared like this: @class MyClass; @protocol MyProtocol; In Objective-C, classes and protocols can be forward-declared if you only need to use them as part of an object pointer type, e.g. MyClass * or id<MyProtocol>.
- it's good practice to use forward declaration instead because you eliminate redundant dependencies by using it. Also note, that when you change the header file, it causes all files that include it to be recompiled.
You can't. The standard just states that FILE
is "an object type capable of recording all the information needed to control a stream"; it's up to the implementation whether this is a typedef
of a struct
(whose name you don't know anyway), or something else.
The only portable way to declare FILE
is with #include <stdio.h>
(or <cstdio>
in C++).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With