struct declaration query

Discussion in 'C Programming' started by CptDondo, Apr 26, 2007.

  1. CptDondo

    CptDondo Guest

    I'm trying to figure out some code that uses structures, structures, and
    more structures...

    It's a bit of rat's nest, and I'm having some trouble sorting it all out.

    The authors use a lot of the following 'declarations' in the various .h
    files:

    struct document;
    struct document_view;
    struct link;
    struct session;
    struct term_event;
    struct terminal;
    struct uri;
    struct conv_table;

    These aren't being used as a part of another structure; they're just
    standalone declarations? statements?

    I can't quite picture what these do.

    What exactly would be the effect of those empty declarations?

    Maybe that will help me trace the code....

    --Yan
    CptDondo, Apr 26, 2007
    #1
    1. Advertising

  2. CptDondo

    Drew Lawson Guest

    In article <>
    CptDondo <> writes:
    >I'm trying to figure out some code that uses structures, structures, and
    >more structures...
    >
    >It's a bit of rat's nest, and I'm having some trouble sorting it all out.
    >
    >The authors use a lot of the following 'declarations' in the various .h
    >files:
    >
    >struct document;
    >struct document_view;
    >struct link;
    >struct session;
    >struct term_event;
    >struct terminal;
    >struct uri;
    >struct conv_table;
    >
    >These aren't being used as a part of another structure; they're just
    >standalone declarations? statements?


    They are incomplete types. Usually, the reason for putting those
    in is that something in the file declares a pointer to one of them:

    struct foo;
    void useFoo(foo *fooPtr);

    The compiler doesn't need the detail of the struct to deal with a
    pointer. Any file that uses the fields inside these structs will
    need a complete type defined.

    Some libraries use this for encapsulation -- they give you a pointer
    that you send back in every call, but no one outside the library
    looks inside the struct.

    >I can't quite picture what these do.
    >
    >What exactly would be the effect of those empty declarations?


    They tell you, "These types exist and you don't need to know what
    is inside them."



    --
    Drew Lawson | "But the senator, while insisting he was not
    | intoxicated, could not explain his nudity."
    Drew Lawson, Apr 26, 2007
    #2
    1. Advertising

  3. CptDondo

    Ian Collins Guest

    CptDondo wrote:
    > I'm trying to figure out some code that uses structures, structures, and
    > more structures...
    >
    > It's a bit of rat's nest, and I'm having some trouble sorting it all out.
    >
    > The authors use a lot of the following 'declarations' in the various .h
    > files:
    >
    > struct document;
    > struct document_view;
    > struct link;
    > struct session;
    > struct term_event;
    > struct terminal;
    > struct uri;
    > struct conv_table;
    >
    > These aren't being used as a part of another structure; they're just
    > standalone declarations? statements?
    >

    They are forward declarations, they let the compiler know that these
    types exit. You will probably see use of pointers to these as function
    parameters or structure members.

    --
    Ian Collins.
    Ian Collins, Apr 26, 2007
    #3
  4. CptDondo

    CptDondo Guest

    Drew Lawson wrote:

    >> I can't quite picture what these do.
    >>
    >> What exactly would be the effect of those empty declarations?

    >
    > They tell you, "These types exist and you don't need to know what
    > is inside them."


    OK, thanks. That explains a lot.

    The code uses a lot of structures with pointers to other structures, and
    macros that define yet more pointers....

    The parts I've figured out make sense... But it sure is complicated. :)

    --Yan
    CptDondo, Apr 26, 2007
    #4
  5. CptDondo

    Old Wolf Guest

    On Apr 27, 7:38 am, (Drew Lawson) wrote:
    > CptDondo <> writes:
    > >The authors use a lot of the following 'declarations' in the various .h
    > >files:

    >
    > >struct document;
    > >struct document_view;

    >
    > They are incomplete types. Usually, the reason for putting those
    > in is that something in the file declares a pointer to one of them:
    >
    > struct foo;
    > void useFoo(foo *fooPtr);


    Should read:
    void useFoo( struct foo *fooPtr );

    If you write this code without the forward declaration, then
    'struct foo' gets declared with "prototype scope", meaning
    that any subsequent file-scope "struct foo" is actually a
    different struct. (Don't ask me why).
    Old Wolf, Apr 26, 2007
    #5
  6. CptDondo

    Drew Lawson Guest

    In article <>
    Old Wolf <> writes:
    >On Apr 27, 7:38 am, (Drew Lawson) wrote:
    >> CptDondo <> writes:
    >> >The authors use a lot of the following 'declarations' in the various .h
    >> >files:

    >>
    >> >struct document;
    >> >struct document_view;

    >>
    >> They are incomplete types. Usually, the reason for putting those
    >> in is that something in the file declares a pointer to one of them:
    >>
    >> struct foo;
    >> void useFoo(foo *fooPtr);

    >
    >Should read:
    > void useFoo( struct foo *fooPtr );


    Sorry about that.
    I'm lazy and hate typing 'struct' all the time, so I have been doing
    typedefs for all my structs forever. That seems to have made my
    posting a bit sloppy.

    >If you write this code without the forward declaration, then
    >'struct foo' gets declared with "prototype scope", meaning
    >that any subsequent file-scope "struct foo" is actually a
    >different struct. (Don't ask me why).


    That's a new one. I'm not sure I should try to understand that on
    a Friday afternoon.


    --
    Drew Lawson For it's not the fall, but landing,
    That will alter your social standing
    Drew Lawson, Apr 27, 2007
    #6
  7. Drew Lawson wrote:
    >
    > In article <>
    > Old Wolf <> writes:

    [...]
    > >If you write this code without the forward declaration, then
    > >'struct foo' gets declared with "prototype scope", meaning
    > >that any subsequent file-scope "struct foo" is actually a
    > >different struct. (Don't ask me why).

    >
    > That's a new one. I'm not sure I should try to understand that on
    > a Friday afternoon.


    In that case, try wrapping your head around this error message:

    usenet.c(14) : warning C4133: 'function' : incompatible types -
    from 'struct foobar *' to 'struct foobar *'

    Here's the source:

    ==========
    int foo(struct foobar *ptfoo)
    {
    return 42;
    }

    struct foobar
    {
    int a;
    int b;
    };

    int bar(struct foobar *ptbar)
    {
    return foo(ptbar);
    }
    ==========

    When compiling in ANSI mode, the "struct foobar *" type of the
    parameter to foo() is not the same "struct foobar *" type of the
    parameter to bar().

    --
    +-------------------------+--------------------+-----------------------+
    | Kenneth J. Brody | www.hvcomputer.com | #include |
    | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------+
    Don't e-mail me at: <mailto:>
    Kenneth Brody, Apr 27, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Noah
    Replies:
    5
    Views:
    947
  2. Alex Vinokur
    Replies:
    4
    Views:
    574
    Jonathan Turkanis
    Apr 5, 2004
  3. Chris Fogelklou
    Replies:
    36
    Views:
    1,357
    Chris Fogelklou
    Apr 20, 2004
  4. Ehud Shapira
    Replies:
    20
    Views:
    1,106
    Ehud Shapira
    Jun 30, 2007
  5. slocum
    Replies:
    3
    Views:
    500
    slocum
    Apr 11, 2008
Loading...

Share This Page