0

This problem is from a solved problem in my old question, which is from: C++ Inserting 2D array Object into another 2D array Object

But also created a new problem for me. Please read the question and the solution in the link to understand my problem. The solution in the previous question was to make my Data Member Function into a pointer to pointer, to allow the pass into the other Data Member Function. But while fixing that, the first Data Member Function which is smallerArray.extractPiece() now only return address of the pointer to the pointer and not the content of those pointers. I need the content in order for my 2nd Data Member Function largerArray.extractArray(result) to work properly, as I attempt run the code and gave an Window Error, and not a Compile Error.

Does anyone know how to extract the content of the smallerArray.extractPiece() and instead of getting of the address, and is there isn't, does anyone have any other methods of creating a 2D-Array Object?

3 Answers 3

1
void Grid::extractArray( int** arr )
{
  for(int i = 0; i < xGrid ; ++i) {
    for (int j = 0; j < yGrid ; ++j) {
      squares[i][j] = arr[i][j];
    }
  }
}

The smaller array int**arr does not have as many elements as the Grid. xGrid and yGrid are too large to use as indices for arr[][].

You must pass the complete smaller array object into the extractArray() function and use the sizes from this object for the copy function.

void Grid::extractArray( const Piece & piece)
{
  for(int i = 0; i < piece.xGrid ; ++i) {
    for (int j = 0; j < piece.yGrid ; ++j) {
      squares[i][j] = arr[i][j];
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Right now, your problem seems a bit underspecified. How large of a 'piece' do you expect from the smaller array, and where in the larger array do you want to insert it?

Comments

0

It may make things easier if you create a 2D array object or class (or struct)

class BaxMatrix {
public:
  int m_Data[4][4];
}

with a little work you could build dynamic structures or use STL structures as desired. The data, and the reference to the data are two different animals. It's best for you to clarify each of their roles in your thinking, before proceeding forward.

Comments

Your Answer

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