615 views #include <stdio.h>
void fun(int a){
char *arr[]={"0000","0001","0010","0011","0100","0101","0110","0111","1000",
"1001","1010","1011","1100","1101","1110","1111"};

unsigned char* p = (unsigned char*)&a; // p is char pointer and  will hold the
//address of a(let 1000). So p is having
//value 1000 and a is having value 10.

p+=3; // p is incremented to 3 now p is having value 1003.

int i;

for(i=0;i<sizeof(a);i++){ //as given in question that integer is 4B so
//this loop will run 4 times.

int d=(*p)>>4; // in first loop p is having value 1003 and at that
//location 0 will be there so we'll do right shift 4
//times on 0 and 0 will be assigned to d.
// Now same 0 will be assigned till p is equal to 1001
//when p will have value 1000 then it will
// point to a. and 10 will be right shifted 4 times
//and 0 again will be assigned to d.

printf("\n%s",arr[d]);//it will print arr which is 0000.
// at the last iteration it will print 0000.

d = (*p)&0xf; // it will do anding with 1111 so again 0
//will be assigned to d.
//in last iteration 10 will be assigned to d.

printf("%s\n",arr[d]);// so it will again print arr which is 0000.
//(till 1001 it will print same 0000)
// in last iteration it will print arr. which is 1010.

p--; // it will decrement p by 1.

}

}

// final output will be printed as 00000000 00000000 00000000 00001010

int main(void) {
int a=10;
fun(a); // a is passed with value 10.
return 0;
}


one small doubt at 1003 location i assumed 0 value will be there but i think any garbage can also present.

The answer depends on the Endianness of the system.

a = 10 , and let &a = 1000

Byte 1003: 00001010 (Little Endian) or 00000000 (Big Endian)
Byte 1002: 00000000
Byte 1001: 00000000
Byte 1000: 00000000 (Little Endian) or 00001010 (Big Endian)

//Consider the assignment
unsigned char* p = (unsigned char*)&a; // p points to Byte 1000, so *p = 0 or *p = 10
p+=3; // p points to Byte 1003, so *p = 10 or *p = 0

//Now consider the for loop
for(i=0;i<sizeof(a);i++){ //as integer is 4 bytes, i = 4
int d=(*p)>>4; // Right Shift is division by power of 2.
//So d = (*p)/2^4 = 0 (doesn't matter if *p is 0 or 10)

// for all the 4 loops, 1st 4 bits will be arr
printf("\n%s",arr[d]);//it will print arr which is 0000.

d = (*p) & 0xf;
// bitwise AND of *p with all 1's --> either 10 is assigned to d or 0 is assigned to d.

printf("%s\n",arr[d]);
// so it will print arr which is 0000, or it will print arr. which is 1010.

p--;
/* it will decrement p by 1. So we go to Byte 1002. This will print arr for both printf's.
The loop after that (Byte 1001) will also print arr for both printf's.
*/
/*
If printf is 00001010 in Byte 1003, then printf will be 00000000 in Byte 0.
This leads to the output 00001010 00000000 00000000 00000000.

If printf is 00000000 in Byte 1003, then printf will be 00001010 in Byte 0.
This leads to the output 00000000 00000000 00000000 00001010.
*/

}

Hence the solution may be either option a (big-endian) or option b (little-endian).

1
549 views