0

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++; 
}
Armino
  • 129
  • 1
  • 1
  • 11
  • `AllAnswers *& oldAnswers` - reference to a pointer, needed when you are changing where pointer points to later `oldAnswers = new AllAnswers[questionsCounter+1];` you cannot do this if you just pass copy of the pointer – Killzone Kid May 19 '18 at 13:08
  • 1
    Possible duplicate of [Reference to a pointer](https://stackoverflow.com/questions/3128662/reference-to-a-pointer) – Killzone Kid May 19 '18 at 13:10

1 Answers1

0

oldAnswers = edits the pointer itself, like you said.

operator[] is actually a function which takes a pointer and returns a reference. When you have allStudents[i] =, you're actually not editing the pointer itself. Instead, you are traveling i Students past the allStudents pointer and editing whatever is there.

So it doesn't matter if allStudents is a copy (pass by reference) or an alias (pass by value) of another pointer, because in either case all students[i] returns the same reference to the same Student.

When not doing coursework, use std::vector instead of new[]. It's a lot more intuitive with regards to value/reference semantics than a pointer into the heap, which comes in handy.

hegel5000
  • 876
  • 1
  • 7
  • 13
  • Why the downvote? If there's an inaccuracy then you should probably point it out. – hegel5000 May 19 '18 at 13:18
  • Yes, it makes perfect sense now, thank you! So in the first case, we are changing stuff, but it's not the pointer we are changing, that's why the effects take place. In the second case, we are changing the pointer, as well as the other stuff. – Armino May 19 '18 at 13:36