incomplete struct in a struct

Discussion in 'C Programming' started by karthikbalaguru, Aug 14, 2007.

  1. Hi,

    struct sky {
    int stars;
    struct ocean *oceanptr;
    };

    struct ocean {
    int waves;
    struct sky *skyptr;
    };

    Will the compiler accept the field declaration struct ocean
    *oceanptr within struct sky ?

    Actually, the compiler has not yet heard of struct ocean till that
    instant of time . ( struct ocean is ``incomplete'' at that point.) .

    It has been stated that Just a empty declaration of the struct like
    below will fix the issue -
    struct sky;
    struct ocean;

    But, How is this possible ? What is happening here internally ? Is it
    really like that ?

    Thx in advans,
    Karthik Balaguru
    karthikbalaguru, Aug 14, 2007
    #1
    1. Advertising

  2. karthikbalaguru

    Chris Dollin Guest

    karthikbalaguru wrote:

    > Hi,
    >
    > struct sky {
    > int stars;
    > struct ocean *oceanptr;
    > };
    >
    > struct ocean {
    > int waves;
    > struct sky *skyptr;
    > };
    >
    > Will the compiler accept the field declaration struct ocean
    > *oceanptr within struct sky ?


    This smells like homework.

    Did you try this? What happened? What does your book/course notes say?

    > Actually, the compiler has not yet heard of struct ocean till that
    > instant of time . ( struct ocean is ``incomplete'' at that point.) .


    Yes.

    > It has been stated that Just a empty declaration of the struct like
    > below will fix the issue -


    What issue?

    > struct sky;
    > struct ocean;
    >
    > But, How is this possible ?


    Compiler writers (and language designers) are dead smart. (Sometimes,
    it must be said, too smart for their own good, or possibly for their
    users' good.)

    > What is happening here internally ?


    The compiler notes that eg `oceanpointer` is a pointer to struct ocean,
    and that it hasn't yet seen a definition for that struct. But it doesn't
    need a definition of the struct to know about pointers to it.

    > Is it really like that ?


    Is what really like what?

    --
    Chris "is this a rhetorical question?" Dollin

    Hewlett-Packard Limited Cain Road, Bracknell, registered no:
    registered office: Berks RG12 1HN 690597 England
    Chris Dollin, Aug 14, 2007
    #2
    1. Advertising

  3. On Tue, 14 Aug 2007 05:39:05 -0700, karthikbalaguru
    <> wrote:

    >Hi,
    >
    >struct sky {
    > int stars;
    > struct ocean *oceanptr;


    The standard requires that all pointers to struct, regardless of the
    type of struct, have the same size, representation, alignment.
    Therefore, even the compiler knows nothing about struct ocean, other
    than that it is a struct of some kind, that is sufficient for the
    compiler to properly place an object of type struct ocean* in struct
    sky.

    The real question is why didn't you just try it on your system. You
    would have had your answer a lot faster than waiting for people on
    Usenet to respond.

    > };
    >
    >struct ocean {
    > int waves;
    > struct sky *skyptr;
    > };
    >
    >Will the compiler accept the field declaration struct ocean
    >*oceanptr within struct sky ?
    >
    >Actually, the compiler has not yet heard of struct ocean till that
    >instant of time . ( struct ocean is ``incomplete'' at that point.) .
    >
    >It has been stated that Just a empty declaration of the struct like
    >below will fix the issue -
    >struct sky;
    >struct ocean;
    >
    >But, How is this possible ? What is happening here internally ? Is it
    >really like that ?
    >
    >Thx in advans,
    >Karthik Balaguru



    Remove del for email
    Barry Schwarz, Aug 18, 2007
    #3
  4. Barry Schwarz wrote:
    [...]
    > The real question is why didn't you just try it on your system. You
    > would have had your answer a lot faster than waiting for people on
    > Usenet to respond.

    [...]

    For the same reason you don't take your compiler's output of "i=i++"
    to be definitive.

    Besides, he also asked:

    > What is happening here internally ?


    And this cannot be answered simply by compiling.

    --
    +-------------------------+--------------------+-----------------------+
    | 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, Aug 20, 2007
    #4
  5. On Aug 14, 6:10 pm, Chris Dollin <> wrote:
    > karthikbalaguru wrote:
    > > Hi,

    >
    > > struct sky {
    > > int stars;
    > > struct ocean *oceanptr;
    > > };

    >
    > > struct ocean {
    > > int waves;
    > > struct sky *skyptr;
    > > };

    >
    > > Will the compiler accept the field declaration struct ocean
    > > *oceanptr within struct sky ?

    >
    > This smells like homework.
    >
    > Did you try this? What happened? What does your book/course notes say?
    >
    > > Actually, the compiler has not yet heard of struct ocean till that
    > > instant of time . ( struct ocean is ``incomplete'' at that point.) .

    >
    > Yes.
    >
    > > It has been stated that Just a empty declaration of the struct like
    > > below will fix the issue -

    >
    > What issue?
    >
    > > struct sky;
    > > struct ocean;

    >
    > > But, How is this possible ?

    >
    > Compiler writers (and language designers) are dead smart. (Sometimes,
    > it must be said, too smart for their own good, or possibly for their
    > users' good.)
    >
    > > What is happening here internally ?

    >
    > The compiler notes that eg `oceanpointer` is a pointer to struct ocean,
    > and that it hasn't yet seen a definition for that struct. But it doesn't
    > need a definition of the struct to know about pointers to it.
    >
    > > Is it really like that ?

    >
    > Is what really like what?
    >
    > --
    > Chris "is this a rhetorical question?" Dollin
    >
    > Hewlett-Packard Limited Cain Road, Bracknell, registered no:
    > registered office: Berks RG12 1HN 690597 England


    Thx for the info.
    That is called "Forward reference to structure ocean" .The reference
    is preceded
    by the struct keyword to resolve potential ambiguity. Using an
    identifier before its declaration is called a forward reference, and
    results in an error, except in few cases and the above 'structure
    case' is one such case which does not throw any error.

    I am sharing some interesting stuff collected from internet w.r.t
    writing optimised code related to this forward reference as below :
    Allowing Forward reference can greatly increase the complexity and
    memory requirements of a compiler, and generally prevents the compiler
    from being implemented in one pass(one pass compilation).

    Thx,
    Karthik Balaguru
    karthikbalaguru, Aug 27, 2007
    #5
  6. karthikbalaguru

    dk Guest

    On Aug 27, 11:53 am, karthikbalaguru <>
    wrote:
    > On Aug 14, 6:10 pm, Chris Dollin <> wrote:
    >
    >
    >
    >
    >
    > > karthikbalaguru wrote:
    > > > Hi,

    >
    > > > struct sky {
    > > > int stars;
    > > > struct ocean *oceanptr;
    > > > };

    >
    > > > struct ocean {
    > > > int waves;
    > > > struct sky *skyptr;
    > > > };

    >
    > > > Will the compiler accept the field declaration struct ocean
    > > > *oceanptr within struct sky ?

    >
    > > This smells like homework.

    >
    > > Did you try this? What happened? What does your book/course notes say?

    >
    > > > Actually, the compiler has not yet heard of struct ocean till that
    > > > instant of time . ( struct ocean is ``incomplete'' at that point.) .

    >
    > > Yes.

    >
    > > > It has been stated that Just a empty declaration of the struct like
    > > > below will fix the issue -

    >
    > > What issue?

    >
    > > > struct sky;
    > > > struct ocean;

    >
    > > > But, How is this possible ?

    >
    > > Compiler writers (and language designers) are dead smart. (Sometimes,
    > > it must be said, too smart for their own good, or possibly for their
    > > users' good.)

    >
    > > > What is happening here internally ?

    >
    > > The compiler notes that eg `oceanpointer` is a pointer to struct ocean,
    > > and that it hasn't yet seen a definition for that struct. But it doesn't
    > > need a definition of the struct to know about pointers to it.

    >
    > > > Is it really like that ?

    >
    > > Is what really like what?

    >
    > > --
    > > Chris "is this a rhetorical question?" Dollin

    >
    > > Hewlett-Packard Limited Cain Road, Bracknell, registered no:
    > > registered office: Berks RG12 1HN 690597 England

    >
    > Thx for the info.
    > That is called "Forward reference to structure ocean" .The reference
    > is preceded
    > by the struct keyword to resolve potential ambiguity. Using an
    > identifier before its declaration is called a forward reference, and
    > results in an error, except in few cases and the above 'structure
    > case' is one such case which does not throw any error.
    >
    > I am sharing some interesting stuff collected from internet w.r.t
    > writing optimised code related to this forward reference as below :
    > Allowing Forward reference can greatly increase the complexity and
    > memory requirements of a compiler, and generally prevents the compiler
    > from being implemented in one pass(one pass compilation).
    >
    > Thx,
    > Karthik Balaguru- Hide quoted text -
    >
    > - Show quoted text -


    "except in few cases and the above 'structure
    case' is one such case which does not throw any error."

    Can you list out other such cases please?

    -Dk
    dk, Aug 29, 2007
    #6
    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. Mohammed  A khader

    Is it incomplete sensitivity list ?

    Mohammed A khader, Mar 2, 2005, in forum: VHDL
    Replies:
    7
    Views:
    1,255
    Jerry Coffin
    Mar 3, 2005
  2. Jurgen
    Replies:
    5
    Views:
    671
    William Brogden
    Aug 19, 2003
  3. rroger

    ODS - Incomplete InputParameters

    rroger, Apr 27, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    322
    rroger
    Apr 27, 2006
  4. Chris Fogelklou
    Replies:
    36
    Views:
    1,357
    Chris Fogelklou
    Apr 20, 2004
  5. John Sasso
    Replies:
    3
    Views:
    835
    SM Ryan
    Jul 16, 2006
Loading...

Share This Page