Is it possible to create a dynamic-sized array of structures within a structure?

To provide some info, I want to read some student info off an input file, which includes some miscellaneous information, subject codes and corresponding marks. The number of subjects taken is also provided as a number in the file itself. My intention is to have the program determine how many structs of subjects are required for each struct of student.


I don't know what are you trying to do
But a dynamic struct within a struct is pretty easy

struct student_info
  std::string name;
  size_t id;
  .......and so on

class student_handle
  public : 
  //define the function you want
  private :
    std::vector<student_info> data_;//dynamic array of struct student_info

you could make the codes become more generic if you need

Thanks for the example, but I don't think that is what I am looking for.

To clarify, I am trying to store an array of "subject" structs within a "student" struct. The size of the array would not be known until the input file is read, so I need to let the array size be based on the number of subjects, which happens to be given in the input file.

Below is an example of the format the input file is in.

10 // student ID
John_Smith // student name
2 // number of subjects taken
ABCD123   75 // subject code and marks (1 line of this per subject)
EFGH456   80

IJKL789   65
MNOP012   85
QRST345   70

looks like you need to read a file(like txt?)
If you want to read a file, you need std::ifstream or std::fstream
but I would suggest for std::ifstream.

struct student_info
  size_t id;
  std::string name;
  size_t subject_num;
  //you could consider another containers according to your need
  std::map<std::string, size_t> subCode_and_marks;
  private :
    //insert the data according to the number of the subject_num
    void insert_subCode_and_marks();

There are a lot of the ways to solve this problem
I don't know is this what you want?
std::map could extend the sizes dynamically
if you want to save them as dynamic array
you could try

std::vector<std::pair<std::string, size_t> > subCode_and_marks;

I hope this could help you

Yes, what you are trying to accomplish is perfectly possible. Let's say you've defined your subject structure as

typedef struct subject_s
//members here
} subject_t, *subject_p_t;

and your student structure as

typedef struct student_s
subject_p_t pSubjects;
int numSubjects;
//other members here
} student_t, *student_p_t;

Once you have defined your student structure (either statically or dynamically), you can just allocate the needed amount of memory by accessing the pSubjects member in your student structure, like so:

student_t someStudent;
someStudent.numSubjects = 5
someStudent.pSubjects = (subject_p_t)malloc(someStudent.numSubjects * sizeof(subject_t);

Since you didn't specify exactly which language you are using, I assumed C. I also added the numSubjects member in your student structure purely for the reason so that you can keep track of the number of subjects allocated for that student.

stereomatching and MonsieurPointer, many thanks to both of you.