1

I am trying segment my matrix 2by2 square matrices. but I'm getting a compilation fault and don't know why. My code:

    int main()
    {
        int rowsize,n,i,j,l,k, var=0,var2=0,count = 0,square2by2[2][2];
        printf("Size of square matrix: " );
        scanf("%d",&n);
        rowsize=n;
        int *matrix = (int *)malloc(rowsize * rowsize * sizeof(int));
        for (i = 0; i <  rowsize; i++)
          for (j = 0; j < rowsize; j++)
             *(matrix + i*rowsize + j) = ++count;

        for (i = 0; i <  rowsize; i++)
         {
            for (j = 0; j < rowsize; j++)
            {
                printf("%d ", *(matrix + i*rowsize + j));
            }
            printf("\n");
         }
         for( i=0, l=var2 ;i<2 , l<var2+rowsize ;i++,l++)
        {
            for( j=0 , k=var ; j<2, k<var+2 ;j++,k++)
            {
                square2by2[i][j]=matrix[l][k]; // error line
            }

            if(i==1)
            {
                var++;
                if(var==rowsize-1) 
                {
                    printf("\n");
                    for(int x=0;x<2;x++)
                    {
                        printf("\n");
                        for(int y=0;y<2;y++)
                        {
                            printf("%d\t",square2by2[x][y]);
                        }
                    }
                    var = 0;
                    i-=2;
                    l-=1;
                    if(l==rowsize+1) 
                    {
                        break;
                    }
                }
                else
                {
                    i-=2;
                    l-=2;
                    printf("\n");
                    for(int x=0;x<2;x++)
                    {
                        printf("\n");
                        for(int y=0;y<2;y++)
                        {
                            printf("%d\t",square2by2[x][y]);
                        }
                    }
                }

            }
return 0;
        }

and the error is

37 36 [Error] invalid types 'int[int]' for array subscript why am i getting this error?

4
  • Did you try to ... Google it? Commented Nov 5, 2015 at 12:36
  • 2
    Possible duplicate of c++ error: invalid types int int for array subscript Commented Nov 5, 2015 at 12:37
  • i did try already but i couldn't solve problem Commented Nov 5, 2015 at 12:37
  • is that really a duplicate? the accepted answer draws a line between C++ and c. But it won't hurt to look into the three answers over there, @pseudocode - maybe one of them can help you in spite of it being a C++ question Commented Nov 5, 2015 at 13:17

2 Answers 2

1

The problem is your declaration for matrix:

int *matrix = ...

This declares matrix to be a pointer to an int, or equivalently a pointer into a one-dimensional int array. The expression matrix[l] is therefore int-valued. You cannot apply the second [k] index to an int.

To do what you're trying to do, you need a pointer into a two-dimensional array. Here's how you can do that:

int a[dim1][dim2];
int (*p)[dim2] = a;    // The parentheses are necessary!

Here p is a pointer into a, so you can reference p just as you would a, e.g. p[i][j].

Sign up to request clarification or add additional context in comments.

Comments

0

matrix has been declared as int* but is being used as int[][]

To fix, use

square2by2[i][j]=matrix[l * rowsize + k];

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.