0

I have a 20x20 matrix. I want to extract chunks of data from the matrix. I have

int theMatrix[20][20] = {};//Full code initializes with #'s
int *theMatrixPointer = &theMatrix;

But then I get a compiler warning saying

warning: initialization from incompatible pointer type

I went ahead and ran the code and it looks to be moving across the matrix from left to right. At least in the short run. The implementation:

//left to right with pointer;
while(theMatrixPointer)
{
    int one = 0;
    int two = 0;
    int three = 0;
    int four = 0;
    double currentProduct = 0;
    //int stop = 0;
    for(int i = 0; i < depth; i++)
    {
        /*if(!theMatrixPointer)
        {
            stop = 1;
            break;
        }*/
        int currentValue = *theMatrixPointer++;
        printf("%d\n",currentValue);
        if(i == 0)
        {
            one = currentValue;
        }
        else if (i == 1)
        {
            two = currentValue;
        }
        else if (i == 2)
        {
            three = currentValue;
        }
        else if (i == 3)
        {
            four = currentValue;
        }
    }
    /*if(stop)
        break;*/
    currentProduct = one * (double)two * three * four;
    printf("PRODUCT: %f\n",currentProduct);
    startPoint++;
    theMatrixPointer = startPoint;
}

...breaks over time as the data is garbage (big ints that are not in the matrix). So, how can I properly iterate this matrix with a pointer?

P.Brian.Mackey
  • 43,228
  • 68
  • 238
  • 348

2 Answers2

1

Firstly, the reason you get the warning is because &theMatrix is of type int(*)[20][20], whereas theMatrixPointer is of type int *. You want this:

int *theMatrixPointer = &theMatrix[0][0];

Secondly, the reason you are getting garbage is because you're going past the end of the array. while (theMatrixPointer) will iterate until theMatrixPointer == 0. But remember that theMatrixPointer is an address. This won't be 0 until you've iterated over the entire address space and wrapped back round!

You are probably best off doing this:

int i, j;
for (i = 0; i < 20; i++)
{
    for (j = 0; j < 20; j++)
    {
        // matrixPointer[i][j] is the current element
    }
}
Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
  • The problem with going the route of i,j index iteration is that the algorithm I planned out doesn't work. I need to pull blocks from the matrix 4 at a time. After each pull the next index needs to go up by one with rinse-repeat. On a similar, but different problem I solved this cleanly with a string and pointers, but this matrix is proving far more challenging. I may need to rethink my approach. – P.Brian.Mackey Apr 28 '11 at 21:03
  • @ssegvic Thanks guys I now know what it takes to do this with pointers. – P.Brian.Mackey Apr 29 '11 at 12:44
1

Check my answer to a similar question here. Basically, I think that dealing with theMatrix[20*20] is a wiser default approach than dealing with theMatrix[20][20].

Community
  • 1
  • 1
ssegvic
  • 3,123
  • 1
  • 20
  • 21