C ++ Inserting a 2D array object into another

This problem is a continuation of the previous problem:

C ++ Returning and inserting a 2D array object

and it is highly recommended to review the link to understand the following.

I followed Adam Rosenfield's answer and solved the first two problems. However, the last problem has not yet been resolved, which is related to the first two. I'm not sure if the problem is in how I am trying to change the code, or if there is a problem in what is being done.

This is part of what is written in int main ():

    int i, j;

    Grid myGrid;
    Piece myPiece;

    //First two lines of Adam Code
    int (*arrayPtr)[4][4] = myPiece.returnPiece();
    int cell = (*arrayPtr)[i][j];

    //compiler error
    myGrid.insertArray(cell); <--- Problem

      

I'm not sure if this is the wrong argument, or if I'm trying to get it wrong. This is what I get when I try to compile:

In function `int main () '
invalid conversion from `int 'to` int (*) [4] [4]'
initializing argument 1 of `void Grid :: insertArray (int (*) [4] [4]) '
[Build Error] [grid test.o] Error 1

I've tried these:

myGrid.insertArray ((* arrayPtr) [4] [4]); // Same Error
myGrid.insertArray ((* arrayPtr) [i] [j]); // Same Error

I'm not sure what the problem is and not sure what to do. I thank Adam and others for their help with previous problems, but does anyone know how to fix this last problem?

"having returnpiece () is accepted as argument to insertArray ();

0


source to share


2 answers


int i, j;

Grid myGrid;
Piece myPiece;

//First two lines of Adam Code
int (*arrayPtr)[4][4] = myPiece.returnPiece();
int cell = (*arrayPtr)[i][j];

//compiler error
myGrid.insertArray(cell); <--- Problem

      

If you want to copy a chunk (or a 4x4 section) you need to pass a pointer to that section The Adam function gave an argument of type (int * [4] [4]), not an integer

 void Grid::InsertArray(int (*arr)[4][4]) {
      for(int i = 0; i < x_ROWS; i++)
      {
          for(int j = 0; j < y_COLUMNS ; j++)
              squares[i][j] = (*arr)[i][j];
      } 
 }

      

to pass it to arrayPtr



int i, j;

 Grid myGrid;
 Piece myPiece;

 //First two lines of Adam Code
 int (*arrayPtr)[4][4] = myPiece.returnPiece();
 myGrid.insertArray(arrayPtr); 

      

or in one line

myGrid.insertArray(myPiece.returnPiece());

      

+1


source


I see several problems:

1) Maybe you just didn't cut and paste everything, but it looks like you are setting cell

like (*arrayPtr)[i][j]

before i and j matter - in C / C ++ they won't be automatically initialized to 0, their values ​​can be anything (often absurd high / low numbers, whatever was previously in memory at that location).



2) The purpose of your insertArray function is to take an array and insert it into another. Therefore, you must pass an array. Instead, you pass it in cell

, which is the only element in the array. You want to give it arrayPtr

yourself. In the first line of code, Adam [4] [4] is part of the array type (it indicates that it is a 4x4 array). But it myGrid.insertArray((*arrayPtr)[4][4]);

means: "Take an element in the 4th row and 4th column of arrayPtr" - outside the variable declaration [4] [4] does the actual search for the element. Different context, different meaning.

3) Style note: Looking at your last post, the code for insertArray does not "insert" in the sense that the word is commonly used in programming. Insertion is when you add an element to a sequence (such as an array or linked list) without erasing anything that came before. Your code is doing a copy, so copyArray or setArray will be less misleading.

0


source







All Articles