I am passing a 2d array to a function to print the output, but the output I am getting is erroneous
function
void PrintArray(unsigned char mat[][4]){
int i, j;
printf("\n");
for(i = 0;i<4;i++){
for(j = 0;j<4;j++)
printf("%3x",mat[i][j]);
printf("\n");
}
printf("\n");
}
main function
int main(){
int i,j;
//static int c=175;
unsigned char state[4][4], key[4][4], expandedKey[176];
printf("enter the value to be decrypted");
for(i=0;i<4;i++)
for(j=0;j<4;j++)
scanf("%x",(unsigned int *)&state[j][i]);
PrintArray(state);
return 0;
}
expected output
1 5 9 c
2 6 0 d
3 7 a e
4 8 b f
actual output
h2o@h2o-Vostro-1015:~$ ./a.out enter the value to be decrypted 1 2 3 4 5 6 7 8 9 0 a b c d e f
1 5 9 c
0 0 0 d
0 0 0 e
0 0 0 f
I checked the method of passing 2d array, its correct I think, but not sure why m getting this output, kindly advise...
scanftreats the pointer into achararray as a pointer tounsigned int? The typecast may silence the warnings, but that doesn't mean the code works as expected.