Declaring a struct before defining it

This is a discussion on Declaring a struct before defining it within the C Programming forums, part of the General Programming Boards category; Hello. How to define structures before using them in each other. main.cpp : Code: #include <iostream> using namespace std; struct ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,673
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 05:58 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21