Thread: How to link 2 structures

  1. #16
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The program has to know what a Student is -- you need to declare it first (not necessarily define, just declare, like
    Code:
    struct Student;
    otherwise you'll get in a big circle)

  2. #17
    Registered User
    Join Date
    Sep 2009
    Posts
    33
    inside the main or outside the main??

    i already have a student structure.. with the attributes.

  3. #18
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Types never go inside main.

  4. #19
    Registered User
    Join Date
    Sep 2009
    Posts
    33
    then how?

    i have a student struct already.. i can add new students, new course, and new sections..

    but can't link them

    maybe addCourseSection(courseSection cs) // something like this??

  5. #20
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You have a circular sort of thing -- Student needs to know that there is such a thing as a CourseSection, and CourseSection needs to know there is such a thing as a Student. So you tell them:
    Code:
    struct Student; //puts the name out there
    struct CourseSection {
      //all the stuff goes here
    };
    struct Student {
      //now we define the thing
    };

  6. #21
    Registered User
    Join Date
    Sep 2009
    Posts
    33
    declare the student struct twice?

  7. #22
    Registered User
    Join Date
    Sep 2009
    Posts
    33
    it compiles without error..
    how can i link the student struct to the course??

    sorry for disturbing.. but i'm just so amateur at this

  8. #23
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by sivapc View Post
    it compiles without error..
    how can i link the student struct to the course??

    sorry for disturbing.. but i'm just so amateur at this
    That answer hasn't changed. You need to store (a pointer to) the course in your student object.

  9. #24
    Registered User
    Join Date
    Sep 2009
    Posts
    33
    courseSection *section

    inside the student struct?

  10. #25
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Why are we starting over, here? You have four pointers to CourseSection things inside your student struct. Set one of them equal to the course you want to add.

  11. #26
    Registered User
    Join Date
    Sep 2009
    Posts
    33
    in the add course section function or in add student function??

  12. #27
    Registered User
    Join Date
    Sep 2009
    Posts
    33
    Code:
    void addStudent ()
    {
        Student *temp;
        temp = new Student;
        
        CourseSection *cs;
        cs = new CourseSection;
        
        cout<<"Enter Course Section:"<<flush;
        
        
        
        
    }
    i'm stuck here..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List, Please Help!
    By CodeMonkeyZ in forum C Programming
    Replies: 5
    Last Post: 02-17-2009, 06:23 AM
  2. I'm confused about link lists (again)
    By JFonseka in forum C Programming
    Replies: 4
    Last Post: 06-13-2008, 08:13 PM
  3. Function to check memory left from malloc and free?
    By Lechx in forum C Programming
    Replies: 4
    Last Post: 04-24-2006, 05:45 AM
  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. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 12:59 PM