I tried to use pointer arithmetic to sort a 2D matrix. The Code I've written was:
Code:
#include<stdio.h>
#include<stdlib.h>
main()
{
int a[3][4], i, j, k, l, temp;
for(i=0; i<3; i++)
{
for(j=0; j<4; j++)
{
scanf("%d", (a+i*4+j));
}
}
for(i=0; i<3; i++)
{
for(j=0; j<4; j++)
{
for(k=0; k<3; k++)
{
for(l=0; l<4; l++)
{
if(*(a+i*4+j)<*(a+k*4+l))
{
temp = *(a+i*4+j);
*(a+i*4+j) = *(a+k*4+l);
*(a+k*4+l) = temp;
}
}
}
}
}
for(i=0; i<3; i++)
{
for(j=0; j<4; j++)
{
printf("%d\t", *(a+i*4+j));
}
printf("\n");
}
return 0;
}
The compiler creates three message:
Line 27: Warning: Assignment makes int to pointer without a cast.
line 28: Incompatible types when assigning to type to type 'int[4]' from type 'int *'
line 29: Incompatible types when assigning to type to type 'int[4]' from type 'int'
I'm new to the whole programming thing. And I can not figure out what is wrong with my program as far my knowledge goes this should not be a problem. Help please.
Thanks in advance. And explain in detail.