How to interpret structure without definition?
Asked Answered
P

5

9

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.

Pecker answered 26/12, 2011 at 6:23 Comment(0)
D
13

This is usually used for one of the following:

  1. 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.

  2. 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.

Denote answered 26/12, 2011 at 6:27 Comment(2)
@user1115935, this is also called forward declaration.Margerymarget
Ok, but how can I know where this struct is defined? Which library? It is just magically there.Amber
S
2

Those are incomplete types or opaque types, and they are useful when the library interface only works with pointers to those types. You don't need to fill in all the details of what is in the structures so no code outside the library can ever access the data in the structure (legitimately). The code inside the library has a private header that supplies the details of what is in the structures, so the library can indeed work with the contents, but that is a detail hidden from the consumers of the library.

Incomplete types provide much better type safety than another alternative that is sometimes used, namely void pointers. It is too easy to get confused when there are multiple different types all represented by 'void *'. With incomplete types, you can properly distinguish between the different types, but still not reveal the details of how they are implemented.

Saddlecloth answered 26/12, 2011 at 6:47 Comment(1)
DBus implementation in Gnome as an example: "The #GDBusConnection structure contains only private data and should only be accessed using the provided API." github.com/GNOME/glib/blob/master/gio/gdbusconnection.cKenwrick
F
2

You should get an introduction to forward declaration in C. Please try this link.

Fante answered 26/12, 2011 at 7:16 Comment(0)
U
0

I found such things in some library's API provided header file. Some library doesn't want to expose the member details of some structure used in library but they intend that the application may use those structures as pointers, in that case they do so as shown...

Udell answered 26/12, 2011 at 6:31 Comment(0)
G
0

Forward Declaration this helps compiler to resolve structure references from different parts of a program.

Grillroom answered 26/12, 2011 at 7:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.