简体   繁体   中英

References in functions with dynamic arrays and structures C++

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++; 
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM