344 views

1 Answer

Best answer
3 votes
3 votes

If we consider int to be 4 bytes here and assuming Little Endian mechanism to store the bytes of the variable ( which is followed in almost all of the architectures ). , as 'i' is declared as int and assigned the value of 511 , so the binary equivalent will be stored as :

00000000  00000000  00000001  11111111

Now the lowermost byte is "11111111" , hence it is stored at lowermost address , say at address 1000..Hence contents of addresses 1000 , 1001 , 1002 and 1003 in that order following Little Endian Notation :

11111111  00000001  00000000  00000000

Now at present , &i contains address of 'i' as an int.

To access the data bytewise (as we know 1 char = 1 byte) , we typecast the integer pointer to char pointer first and then dereference it which is done by step :    char * ptr  =  (char*) &i ;

Hence now on dereferencing we will get the data bytewise..

Hence now on dereferencing data at location 1000 , we will get 1 B data instead of 4 B..

Hence *ptr will give us value  =   decimal equivalent of 11111111   =   255

But we know that data is stored is stored in 2's complement form..

Hence we need to take 2's complement of the bit pattern to get true magnitude .

Hence -1 should be the correct answer..

edited by

Related questions

0 votes
0 votes
1 answer
1
shree asked Nov 22, 2014
501 views
What will be the second value printed by the program if parameter passing mechanism is call by reference?int b=10 //global begin procedure func(int x,int y) begin print(b...
3 votes
3 votes
2 answers
2
sabir asked Nov 24, 2015
867 views
Main () { int a [3] [4] = $\begin{pmatrix} 1&2&3&4 \\ 5&6&7&8 \\ 9&10&11&12 \\ \end{pmatrix}$ printf ("\n% u% u% u", a[0]+1, * (a[0] + 1), *(*(a + 0)+1)); }What is the o...
0 votes
0 votes
0 answers
3
cse.Kumar asked Dec 27, 2017
162 views
0 votes
0 votes
4 answers
4
Sanjay Sharma asked Jun 7, 2016
1,909 views
If a= 10 then what will be the value of b in a C language code.If b= a++ + a++ + a++;.30.23.33.Undefined behaviour