Thread: Structures, which comes first: the chicken or the egg?

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    6

    Structures, which comes first: the chicken or the egg?

    Hi guys,
    I'm new to the forum so nice to meet you all

    I've started programming in C not long ago and had an assignment to write a creative program implementing sturctures.

    well, I've started writing it and then came across this problem:

    Code:
    #include <stdio.h>
    
    
    
    
    struct Personal_Details
    {
        char name[20];
        char nickName[10];
        int age;
        int kills; //the amount of men he killed
    };
    struct Consigliere //adviser to the boss
    {
        Personal_Details PD;
        int relationship_boss; //How close is the consigliere to the boss from 1 to 10 (10-as close as he'll ever get without being killed).
        Boss boss;
    };
    struct Boss
    {
        Personal_Details PD;
        Consigliere consigliere;
    
    
    };
    struct Soldier
    {
        Personal_Details PD;
        Capo related_capo;
        int promotion; //How close to promotion (1- No way ever- too stupid. 10- The boss already knows his name)
        Associate associates[15];
    };
    struct Capo
    {
        Personal_Details PD;
        int loyalty; //(1- already providing information to the cops. 10- will give his life for the familia(the mafia))
        Soldier soldiers[7];
        
    };
    struct Under_Boss
    {
        
        Personal_Details PD;
        int potential; //Potential of being a smart and fearsome boss from 1 to 10 (10- Very talented, half the soldiers already look to him as the actual boss).
        int ambitious; //(10- Probably already plotting how to take down the boss)
        Boss boss;
        static Capo capos[5];
    
    
    };
    
    
    
    
    struct Associate{
        Personal_Details PD;
        Soldier related_soldier;
        int joining_the_familia;//How close to join the familia(1- Only a tool, probably will be killed this year. 10-The underboss already knows his name)
    };
    
    
    struct Sicilian_Mafia // "Try to be as creative as you can"
    {
        char state_of_control[15];
        Boss boss;
        Consigliere consigliere;
        Under_Boss underBoss;
    };
    
    
    void main(){
        Sicilian_Mafia maf;
    };

    I've been trying to replace the sturctures one before the other but it wouldn't help, it always happens that a sturcture needs a structure that haven't been defined yet.

    What shall I do?

    Thanks!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Here is an idea: instead of storing an object of the struct type in another struct, what if you store a pointer to the struct type instead?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    6
    hmm, all right.
    but if the structure who is being pointed at hasn't been declared yet, what happens then?

    Thanks!

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You get an error. However, you can forward declare the struct, e.g.,
    Code:
    struct foo;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2012
    Posts
    6
    you mean like this?

    Code:
    struct Consigliere //adviser to the boss
    {
    	Personal_Details PD;
    	int relationship_boss; //How close is the consigliere to the boss from 1 to 10 (10-as close as he'll ever get without being killed).
    	struct Boss;
    	Boss *boss;
    };
    struct Boss
    {
    	Personal_Details PD;
    	Consigliere consigliere;
    
    
    };
    It still won't help (I guess I'm doing it all wrong : \ ).
    What happens when you try to just declare an empty structure (if it's even possible) and then redeclare it with parameters?

    Thanks again!

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by mapisto View Post
    you mean like this?
    It looks almost right. Try it like this:

    Code:
    struct Boss;
    
    struct Consigliere //adviser to the boss
    {
        Personal_Details PD;
        int relationship_boss; //How close is the consigliere to the boss from 1 to 10 (10-as close as he'll ever get without being killed).
        struct Boss *boss;
    };
    struct Boss
    {
        Personal_Details PD;
        Consigliere consigliere;
    
    
    };

  7. #7
    Registered User
    Join Date
    Dec 2012
    Posts
    6
    Ok,
    I've seen many examples now. none of which explains why this shouldn't work:

    Code:
    #include <stdio.h>
    
    
     
    struct Personal_Details
    {
    	char name[20];
    	char nickName[10];
    	int age;
    	int kills; //the amount of men he killed
    };
    
    
    struct Boss;
     
    struct Consigliere //adviser to the boss
    {
        Personal_Details PD;
        int relationship_boss; //How close is the consigliere to the boss from 1 to 10 (10-as close as he'll ever get without being killed).
        struct Boss *boss;
    };
    struct Boss
    {
        Personal_Details PD;
        Consigliere consigliere;
     
    };
    I've got the idea that you need to forward declare a structure before using it and even then you have to use it with pointers until it's properly defined.

    So what have I done wrong? o_O

    Thanks for your patience people!

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    How does it not work?

    By the way, it probably does not make sense for a boss to own a consigliere object, e.g., there may be a boss who does not have a consigliere for some reason.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You probably need to do this (note the bits I've added in red)
    Code:
    #include <stdio.h>
    
    struct Personal_Details
    {
        char name[20];
        char nickName[10];
        int age;
        int kills; //the amount of men he killed
    };
     
     
    struct Boss;
      
    struct Consigliere //adviser to the boss
    {
        struct Personal_Details PD;
        int relationship_boss; //How close is the consigliere to the boss from 1 to 10 (10-as close as he'll ever get without being killed).
        struct Boss *boss;
    };
    struct Boss
    {
        struct Personal_Details PD;
        struct Consigliere consigliere;
      
    };
    Leaving out the struct keyword when using struct types is a feature of C++, not C (unless that changed in C11, but IIRC it didn't - noting I don't have a copy of that standard handy right now).

    If that doesn't fix your problem, try posting a small but complete sample of code that is not working.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  10. #10
    Registered User
    Join Date
    Dec 2012
    Posts
    6
    In the mafia, a boss must have his advisor (as far as I know) because he's the only 1 who can question his commands without being at risk for it.
    Well, I've never been in the mafia, so I might be wrong ^^

  11. #11
    Registered User
    Join Date
    Dec 2012
    Posts
    6
    grumpy,
    you solved my problem, so TNX !! (and thank you all!)
    I wasn't aware of that difference between the 2 languages.

  12. #12
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    You can still use the types you create without the keyword struct first, you simply need to "typedef" them to do that. That is done
    by using the typedef keyword, and some minor alterations to what you have currently. Here is an example:

    Code:
    typedef struct Boss Boss;
    typedef struct Employee_Data Employee_Data;
    
    struct Boss {
      Employee_Data Boss;
    };
    
    struct Employee_Data {
      char name[MAX_NAME_LEN+1];
      double salary;
      enum { NEVER=1, LITTLEMORE=5, RAISE=10 } chance_of_raise;
      Boss * boss;
    };

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Chicken or the Egg problem...
    By Imperito in forum C++ Programming
    Replies: 2
    Last Post: 08-19-2002, 06:22 AM
  2. Chicken sound
    By grade10 in forum C++ Programming
    Replies: 1
    Last Post: 03-21-2002, 12:05 PM
  3. Chicken Or The Egg
    By gnu-ehacks in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 11-27-2001, 08:30 PM
  4. {chicken Lips}
    By -KEN- in forum C# Programming
    Replies: 2
    Last Post: 11-02-2001, 02:05 AM
  5. { chicken lips }
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 10-09-2001, 04:44 PM