I'm having trouble understanding the theory between what is happening with my two functions in C++. Unfortunately, I was unable to copy the whole code, because I would have to translate all of it from my native language to English. the dilemma I have here is the following: - both allStudents and oldAnswers are dynamic arrays - the function dataEntry works pefectly fine the way it is, it changes the allStudents, and the change is effective in the main function, although the arguments were dataEntry (Student * allStudents...) and not dataEntry (Student *& allStudents...) - in order to get the function addNewAnswer to effectively change the pointer oldAnswers in main function, I have to define the arguments with &, so addNewAnswer (AllAnswers *& oldAnswers...)
Why does one work without the & and the other one doesn't, although the both effectively make changes to the pointers? Is it because the function addNewAnswer also makes changes to the arrays size (memory allocation)?
int questionsCounter = 5;
enum Answers { CORRECT, INCORRECT };
struct Student {
int _stNumber;
char _name[30];
int _year;
Answers *_answers;
char * _userName;
char *_password;
};
struct AllAnswers {
int AnswerNumber;
char *Question;
Answers correctAnswer;
};
void dataEntry(Student * allStudents, int max) {
for (int i = 0; i<max; i++) {
cout << "\t::STUDENT " << i + 1 << "::";
cout << "Enter Student's name: ";
cin.getline(allStudents[i]._name, 30);
cout << "Enter Student's number: ";
cin >> allStudents[i]._stNumber;
cout << "Enter Student's year (1,2,3,4): ";
cin >> allStudents[i]._year;
allStudents[i]._userName = new char[11];
allStudents[i]._userName = GetUserName(allStudents[i]);
allStudents[i]._password = nullptr;
changePassword(allStudents[i]);
}
}
void addNewAnswer (AllAnswers *& oldAnswers, AllAnswers newAnswer) {
AllAnswers *temp = new AllAnswers[questionsCounter+1];
for (int i = 0; i < questionsCounter; i++)
{
copyAnswer(oldAnswers[i], temp[i]);
}
copyAnswer(newAnswer, temp[questionsCounter]);
deallocateAnswers(oldAnswers);
assert(oldAnswers != NULL);
oldAnswers = new AllAnswers[questionsCounter+1];
for (int i = 0; i < questionsCounter+1; i++)
{
copyAnswer(temp[i], oldAnswers[i]);
}
questionsCounter++;
}