0

Possible Duplicate:
How do I work with dynamic multi-dimensional arrays in C?
Static Matrix Casting to a pointer

my this code is throwing exception at line

           resMat[i][j] = matp[i][j];

// code begins here

    #include <iostream>
    #define MOD 1000000007
    #define LL long long
    using namespace std;

    int mat[6][64][64],mat2[2][2], mat4[4][4], mat8[8][8], mat16[16][16], mat32[32][32], mat64[64][64];
    int **point[6];
    int resMat[64][64];

    void fooMatrix(int **matp, int size)
    {
        int i,j;
        // find canFollow of all matrixes   
        for(i=0;i<size;++i)
        {
            for(j=0;j<size;++j)
            {
      // throwing exception here
                resMat[i][j] = matp[i][j];
            }
        }
    }



    int main()
    {
        point[0] = (int **)mat2;
        point[1] = (int **)mat4;
        point[2] = (int **)mat8;
        point[3] = (int **)mat16;
        point[4] = (int **)mat32;
        point[5] = (int **)mat64;

        LL a,b,res;
        scanf("%lld %lld",&a,&b);
        fooMatrix(point[a-1],1<<a);
                    return 0;
    }

i want to process on different sized matrices of int in my function fooMatrix like say store it in resMat. Help me resolve this issue.

I am using DevC++ (g++ compiler) in windows.

Community
  • 1
  • 1
Ashish Negi
  • 5,193
  • 8
  • 51
  • 95
  • where you are calling the function fooMatrix? – Debobroto Das Dec 02 '12 at 05:34
  • 1
    All of your casts are wrong. Remove them. – melpomene Dec 02 '12 at 05:36
  • if you dump the #include and using namespace std; for #include , what does it do when compiled as c instead of c++? – technosaurus Dec 02 '12 at 05:36
  • 1
    If you are passing one of `mat2`, `mat4`, etc... as `matp` then it isn't working because those types (`int**` and `int[][]`) describe different layouts of data in memory. Which is to say that arrays and pointers are *not* the same thing no matter what anybody told you. BTW---the fact that you have to cast to make the assignments `point[2] = (int **)mat8;` is a hint: you have broken the type system. – dmckee --- ex-moderator kitten Dec 02 '12 at 05:38
  • i think foomatrix is findallmatrix, posting cut/paste typo? – technosaurus Dec 02 '12 at 05:38
  • @Das i am sorry.. i edited the changes – Ashish Negi Dec 02 '12 at 05:46
  • 1
    There have been [many question of the years on the passing of multidimensional array to functions](http://stackoverflow.com/search?q=[c%2B%2B]+dimension+array+function&submit=search). There have been [a bunch concentrating on c](http://stackoverflow.com/search?q=[c]+dimension+array+function&submit=search) too which might have more useful answers if you must use raw arrays and pointer like that. – dmckee --- ex-moderator kitten Dec 02 '12 at 05:47
  • @technosaurus it is still throwing exception in c – Ashish Negi Dec 02 '12 at 05:48
  • Yeah. I see the edits. You can't do that. [My best answer on the subject](http://stackoverflow.com/questions/917783/how-do-i-work-with-dynamic-multi-dimensional-arrays-in-c/918121#918121) is c-specific, but it applies to c++ too. – dmckee --- ex-moderator kitten Dec 02 '12 at 05:48
  • to his credit he was at least trying to give his foo names something more useful to make it less of a PITA to read, but back to foo names now - doesn't bother me, I'm good with a,b,c. sometimes proper names trick your brain – technosaurus Dec 02 '12 at 05:57

2 Answers2

1

Reading from the comments and links above i learnt this :

matrix represntation [][] and pointers representation ** is different. Compiler was giving me warning.

2d matrix is not array of pointers. so look at the code below

    #include <stdio.h>
#define MOD 1000000007
#define LL long long

int mat[6][64][64],mat2[2][2], mat4[4][4], mat8[8][8], mat16[16][16], mat32[32][32], mat64[64][64];

// see this is array of single pointer
int *point[6];

int resMat[64][64];

void fooMatrix(int *matp, int size)
{
    int i,j;
    // find canFollow of all matrixes   
    for(i=0;i<size;++i)
    {
        for(j=0;j<size;++j)
        {
               // this is how we would access the matrix.
            resMat[i][j] = matp[i*size+j];
        }
    }
}

int main()
{
    point[0] = &mat2[0][0];
    point[1] = &mat4[0][0];
    point[2] = &mat8[0][0];
    point[3] = &mat16[0][0];
    point[4] = &mat32[0][0];
    point[5] = &mat64[0][0];

    LL a,b,res;
    scanf("%lld %lld",&a,&b);
    fooMatrix(point[a-1],1<<a);
    return 0;
}
Ashish Negi
  • 5,193
  • 8
  • 51
  • 95
0

Instead of using matrices, you will have to use dynamically allocated arrays of pointers to arrays.

You can replace the declarations at the top of the file with the following:

int** newMat(int a, int b){
  int** result = new int*[a];
  for(int i=0; i<a; ++i)
    result[i] = new int[b];
  return result;
}

int** mat2 = newMat(2,2);
int** mat4 = newMat(4,4);
int** mat8 = newMat(8,8);
int** mat16 = newMat(16,16);
int** mat32 = newMat(32,32);
int** mat64 = newMat(64,64);
int*** point = new int**[6];
int** resMat= newMat(64,64);

And then change the assignments at the top of main with:

  point[0] = mat2;
  point[1] = mat4;
  point[2] = mat8;
  point[3] = mat16;
  point[4] = mat32;
  point[5] = mat64;
Eamonn O'Brien-Strain
  • 3,352
  • 1
  • 23
  • 33