0

So I am writing quite a long code and now I came to a problem. How to change structure's arrray's values and get them back to main function. What I am trying to do this function is: to insert a new value at first array spot(which is 0). The whole for cycle frees the Z[0] and works fine. But I do not know how to return whole CHANGED structure array to the main program. How to do this?

Thank you. P.S. sorry, for my broken english.

Here are the neccessary parts of the code:

void insertion(Player Z[], int size); //declaration of the funcion
...
int main()
{
    int size=5;
    Player *Z = new Player[size];
    Insertion(Z, size); //calling the function
    ...
}

void Insertion(Player Z[], int size) //function
{
    size++;
    Player* temp = new Player[size];

    for(int i=0;i<=size-1;i++)
    {
       temp[i+1]=Z[i];
    }

    delete [] Z;
    Z = temp;
    cin>>Z[0].name;
    cin>>Z[0].surname;
}
3
  • 2
    Use std::vector<Player>. Commented Mar 21, 2015 at 21:21
  • I am new at C++ programming. I understand that std::vector is used to change arrays size. But does it help in any way to return array's value from function? Where I can find the best information about std::vector or you can give a simple example how to use it in this case ? Commented Mar 21, 2015 at 21:30
  • @Rokelio: en.cppreference.com/w/cpp/container/vector. However, every C++ book must cover std::vector. If yours does not, then throw it away. std::vector is one of the C++ standard container classes. It's part of the language. You use it by default for every problem of the "I have a collection of X things, and X will only be known when the program runs" kind. Do not use new[]. Commented Mar 21, 2015 at 21:34

1 Answer 1

2

I see many problems in your code.

For example, why are you allocating an array of 5 Players, then deallocating the array and allocating the array again, having the same size? What's the benefit?

You should also note that C++ has call-by-value semantics so the delete[] Z and Z = temp lines have no effect outside of the function you're calling. So, that function should have a signature void Insertion(Player **Z, int size); or Player *Insertion(Player *Z, int size) if you don't want to modify the argument in-place but instead return the new value.

The comments suggested using std::vector. I heavily recommend that approach. If you need a variable-sized container, std::vector is the best choice.

If you however have a reason to do the manual allocations yourself (such as homework assignment) you should use the following strategy: maintain the size of the array in addition to its capacity, and whenever the size would be greater than the capacity, allocate a new array with twice the capacity, copy the contents and deallocate the old array.

Sign up to request clarification or add additional context in comments.

1 Comment

"So, that function should have a signature void Insertion(Player **Z, int size);" That's still call-by-value. It's just that the value that is passed happens to be a double pointer. You can get call-by-reference by simply using a reference to a pointer: void Insertion(Player*& Z, int size). Of course, that's not relevant in practice, as std::vector solves the entire problem in a vastly superior way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.