mxyz
mxyz

Reputation: 63

Struct inside a struct

I have struct

struct Course{
  int cId;
  string courseName;
};

and I want to add students for each Course. I thought to define another struct in to struct Course like

struct Course{
      int cId;
      string courseName;
      struct Student{
         int sId;
         string studentName;
      };
    };

if I define struct like this how could I use it ? I have Course * cptr = new Course[1]; which for using the struct course. How could I add Students to specified cId's ?

Upvotes: -1

Views: 289

Answers (5)

Mr.Mustache
Mr.Mustache

Reputation: 16

I prefer you to divide this struct into two individual struct. Then you can initialize a student object in the Course struct. Or u can implement a function in Course struct to link with Student struct.

Upvotes: 0

Michael Goldshteyn
Michael Goldshteyn

Reputation: 74360

std::vector is your friend, but if you are not allowed to use that, you can do:

struct Student {...}; // Implementation omitted for brevity...

struct Course
{
  Student *students_; // Use students_=new Student[num_students]; to allocate
                      // memory dynamically
};

Upvotes: 0

iavr
iavr

Reputation: 7637

A very simple way:

struct Student
{
    int sId;
    string studentName;
};

struct Course
{
    int cId;
    string courseName;
    std::vector <Student> students;
};

This way you may write

Student s;
Course c;
c.students.push_back(s);

Upvotes: 0

Zac Howland
Zac Howland

Reputation: 15872

struct Course
{
    // ...
    struct Student
    {
        int sId;
        string studentName;
    };
};

This defines a struct Course and another struct Course::Student, but Course does not instantiate any student (there is no member of Course which has a type Student). If you want students to be a member of course, you need something like this:

struct Course
{
    // ...
    struct Student
    {
        int sId;
        string studentName;
    } student;
};

or

struct Course
{
    // ...
    struct Student
    {
        int sId;
        string studentName;
    } students[10];
};

Which would define a single student or an array of 10 students as members of Course, respectively. (NOTE: std::vector would be a better choice than a statically sized array)

Alternatively, you can declare Student as a struct outside of Course:

struct Student { ... };

struct Course
{
    // ...
    Student student;
    // or std::vector<Student> students;
    // or std::array<Student, 10> students;
    // or Student* students
};

Upvotes: 0

juanchopanza
juanchopanza

Reputation: 227390

Your Course does not contain a Student. It just defines a struct of that name, i.e. a type Course::Student. For a type to contain an instance of another type, you just have to declare a member variable:

struct Student { .... };

struct Course
{
  ....
  Student student;
};

If you want each course to hold more than one Student, then you can use a container. In the absence of more information, the best candidate for that is std::vector:

struct Course
{
  ....
  std::vector<Student> students;
};

Upvotes: 5

Related Questions