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?