1

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...

haccks
106k28 gold badges181 silver badges273 bronze badges
asked Aug 25, 2014 at 18:02
4
  • 1
    what output do you expect? Commented Aug 25, 2014 at 18:04
  • @Beed edited the ques, simply entering the array by user and displaying it on console.. Commented Aug 25, 2014 at 18:08
  • What do you think happens when scanf treats the pointer into a char array as a pointer to unsigned int? The typecast may silence the warnings, but that doesn't mean the code works as expected. Commented Aug 25, 2014 at 18:13
  • @WhozCraig it doesn't matter.. does it? if u look at the code it doesnt matter at all, furthermore the expected output is according to [j][i] index. Commented Aug 25, 2014 at 18:18

2 Answers 2

6

I'm going to go out on a limb and say that your problem lies here:

scanf("%x",(unsigned int *)&state[j][i]);

state[i][j] is sized to hold a single char, but you're telling scanf to treat it as a pointer to unsigned int; this likely means that scanf is overwriting adjacent array elements, since sizeof (unsigned int) is most likely greater than sizeof (char).

Change the declaration of the array from char to unsigned int in both main and PrintArray, and lose the cast in scanf.

answered Aug 25, 2014 at 18:13
Sign up to request clarification or add additional context in comments.

1 Comment

Yep, that's it. As alternative suggestion, if the OP doesn't want to change the type of state, the reading code could use scanf to a local unsigned int and then assign its value to state[j][i].
4

The array passing is correct. However, the scanf function seems to overwrite some values to be 0 due to the variable type %x.

The data type specified by %x is "int" because %x is like %d (except that the input is hexadecimal). The data occupies 4 bytes (typically). So when the user enters a number, say, 1, four bytes 01 00 00 00 (assuming little-endianness on an Intel machine) will be written to memory instead of 1. The trailing 0s will erase some existing elements that are stored in the byte array, because in the byte array, each element is allocated only 1 byte.

Try the following code:

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");
int tmp;
for(i=0;i<4;i++)
 for(j=0;j<4;j++) {
 scanf("%x", &tmp);
 state[j][i] = (char)tmp;
 }
PrintArray(state);
nobody
20.3k17 gold badges59 silver badges80 bronze badges
answered Aug 25, 2014 at 18:07

2 Comments

it worked like a charm :) , can u please explain the reason behind this overwriting due to %x, thanks a lot...
Sure. The data type specified by %x is "int" because %x is like %d (except that the input is hexadecimal). The data occupies 4 bytes (typically). So when the user enters a number, say, 1, four bytes 01 00 00 00 (assuming little-endianness on an Intel machine) will be written to memory instead of 1. The trailing 0s will erase some existing elements that are stored in the byte array, because in the byte array, each element is allocated only 1 byte. Hope I explained it more clearly now. :)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.