views:

454

answers:

2

This problem is a continuation of a previous problem:

http://stackoverflow.com/questions/402432/c-returning-and-inserting-a-2d-array-object

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

I followed through Adam Rosenfield's answer and it solved the first two problems. However the last problem is not yet to be solved which high involves on the first two. I am uncertain if the problem is how I attempt to right the code, or if there is a problem in what is being attempted.

This is a section of what is written in the int main():

    int i, j;

    Grid myGrid;
    Piece myPiece;

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

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

I am uncertain if it is the argument that is wrong, or if it is something that I'm attempting that is wrong. This is what I receive when I tried 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 have tried these:


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

I am unsure what is the problem and uncertain on what to do. I thank Adam and the other for helping me with the previous problems, but does anyone know how to solve this last problem?

"having returnpiece() be accepted in the argument of insertArray();

A: 

I see a few issues:

1) It's possible you simply haven't cut and paste everything, but it looks like you're setting cell to be (*arrayPtr)[i][j] before i and j have a value -- in C/C++ they are not automatically initialized to 0, their values could be anything (often absurdly high/low numbers, whatever happened to be in memory at that location before).

2) The purpose of your insertArray function is to take an array and insert it into another. So you should be passing it an array. Instead you're passing it cell, which is a single element of an array. You want to give it arrayPtr by itself. In Adam's first line of code, the [4][4] is part the type of the array (it specifies that it's a 4x4 array specifically). But myGrid.insertArray((*arrayPtr)[4][4]); means, "Take the element in the 4th row and 4th column of arrayPtr" -- outside a variable declaration the [4][4] performs an actual look up of the element. Different context, different meaning.

3) Style note: Looking at your last post, the code for insertArray doesn't perform an 'insertion' in the sense that the word is usually used in programming. An insertion is when you add an element to a sequence (an array or linked list for example) without erasing anything that was present before. Your code performs a copy, so copyArray or setArray would be less misleading.

Joseph Garvin
+1  A: 
int i, j;

Grid myGrid;
Piece myPiece;

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

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

If you want to copy a piece (or 4x4 section), you have to pass a pointer to that section The function Adam gave takes an argument of type (int *[4][4]), not 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];
      } 
 }

so you'd pass it arrayPtr

int i, j;

 Grid myGrid;
 Piece myPiece;

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

or in one line

myGrid.insertArray(myPiece.returnPiece());
Mark Essel