Today I started working on FUSE opensource code, there i found few lines like this:
struct fuse_session;
struct fuse_chan;
I don't know how to interpret it , as far as i knew 'struct
' is followed by structure name and then the name of variable. Where as in this case there is only one named entity followed by struct
,so this fuse session is structure variable or structure name itself.It may be some really basic thing but i am not able to find it anywhere.
This is usually used for one of the following:
You want to let the user of some module to know that this struct exist, but don't want to expose it's content. For example:
in api.h:
struct bar;
void GetStruct(struct bar *);
void SetActive(struct bar *, char);
in your internal file:
struct bar {
char is_active;
};
void GetStruct(struct bar * st) {
st = malloc(sizeof(struct bar));
}
void SetActive(struct bar * st, char active) {
if (st != NULL)
st->is_active = active;
}
This way you encapsulate the implementation and may change the struct later if needed without any impact on the using module.
You want to use a structure (pointer) before declaring it. For example:
struct bar;
typedef int (*FooFuncType)(struct bar *);
struct bar {
FooFuncType a_func;
};
One important note:
If you only have the struct declaration, as in your question, you cannot refer to the struct directly, nor you may use the sizeof
operator, only declare a pointer to the structure.