Thread: Declaring a struct before defining it

  1. #1
    Registered User
    Join Date
    Jan 2005
    Location
    Estonia
    Posts
    131

    Declaring a struct before defining it

    Hello.

    How to define structures before using them in each other.

    main.cpp:
    Code:
    #include <iostream>
    using namespace std;
    
    struct Struct2;
    struct Struct1;
    
    struct Struct2
    {
        Struct1 a;
    }
    
    struct Struct1
    {
        Struct2 b;
    };
    
    int main()
    {
    }
    Compiling with command: g++ main.cpp
    Errors:
    main.cpp:10: error: field `a' has incomplete type
    main.cpp:17: error: multiple types in one declaration


    Thanks in advance.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    for c use gcc and .c extention

    C has no automatic typedef
    Code:
    struct Struct2;
    struct Struct1;
    
    struct Struct2
    {
        struct Struct1* a;
    }
    you can define pointer to undefined struct
    but if you want to add struct as object - it should be fully defined, otherwise compiler cannot know its size.

    What you trying to do will require from the compiler to build endless loop of structs inside structs. It is impossible to do. This data strycture will require infinite memory size
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Jan 2005
    Location
    Estonia
    Posts
    131
    Code:
    #include <stdio.h>
    
    struct Struct2;
    struct Struct1;
    
    struct Struct1
    {
        Struct2 a;
    }
    
    struct Struct2
    {
        int a;
    }
    
    int main()
    {
    }
    ... It stil lwon't compile. Even though it will not produce an infinite loop.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    One of them has to be a pointer to a struct, not an instance of the struct.

    Otherwise, the mutual recursion of the struct uses up all available memory!

    You only need
    struct foo;
    in order to be able to declare a pointer to it.
    At this stage, this is called an incomplete type.


    But you need the whole thing in order to be able to create an instance of that struct.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Location
    Estonia
    Posts
    131
    Ok, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  2. Concatenating in linked list
    By drater in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 11:10 PM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM