I want to create a function that creates an array of structures. What I have done is the following:
struct Student {
char studentNames[128];
unsigned int FN;
short selectiveDisciplinesList[10];
unsigned short countOfSelectiveDisciplines;
bool hasTakenExams;
};
void fillInStudentInfo(Student &student) {...}
Student createStudentsArray() {
unsigned int countOfStudents;
std::cout << "Enter the number of students You are going to write down: " << std::endl;
std::cin >> countOfStudents;
Student studentsArray[countOfStudents];
for (int i = 0; i < countOfStudents; ++i) {
fillInStudentInfo(studentsArray[i]);
}
return *studentsArray;
}
However, when in main()
, I assign:
Student studentArray = createStudentsArray();
I can not access the members of each different struct (f.e. studentArray[i].something
).
Where does the issue come from?
If I do it like this:
Student * createStudentsArray() {
...
return studentsArray;
}
with
Student * studentArray = createStudentsArray();
My IDE warns me:
Address of stack memory associated with local variable returned
But I do seem to be able to access the members of each struct. Is that a bad thing?