1,468 views
0 votes
0 votes
What is the value of the following expression?

 

main()

{  

 int i = 257;  int *iPtr = &i;  

 printf("%d %d", *((char*)iPtr), *((char*)iPtr+1) ); }  

 

 (A) 1 1   (B)1 2     (C) 2 3  

Ans given option a pls explain.

2 Answers

4 votes
4 votes

Option a is right

3 votes
3 votes

let assume integer takes 2 Bytes, Character takes 1 Byte and Pointer (Any type) = 4 Bytes

i=257 = 256+1 ===> 00000001 00000001 ====> For this example, O/p can not effect by endienes of system.

Values 00000001 00000001
Memory Address 101 100

 

 *( (char*)iPtr ) ===> 1) iPtr converted in Character pointer (note that character pointer points only 1 Byte Memory where integer Pointer points 2 Bytes of Memory, actually depends upon size of the datatype)

 ===============> 2) returns the value at that Memory Address which is pointed by the iPtr

 

 *( (char*)iPtr +1 ) ===> 1) iPtr converted in Character pointer (note that character pointer points only 1 Byte Memory where integer Pointer points 2 Bytes of Memory, actually depends upon size of the datatype)

 ===============> 2)increment the address by one 

 ===============> 3) returns the value at that Memory Address which is pointed by the iPtr

 

o/p is 1 1

 

more details https://www.geeksforgeeks.org/little-and-big-endian-mystery/

Related questions

0 votes
0 votes
1 answer
1
Psnjit asked Jan 12, 2019
1,177 views
main(){unsigned int i= 255;char *p= &i;int j= *p;printf("%d\n", j);unsigned int k= *p;printf("%d", k);} Both the outputs are -1. I have even tried with - int i = 255(3rd ...
3 votes
3 votes
2 answers
2
Shamim Ahmed asked Dec 11, 2018
595 views
char *a = “MADEEASY”;char *b = “GATECSIT2019”;char *r = a;char *s = b;printf(“%d”, (int) strlen (b+3[r] – 1[s]));return 0; Whats the output? Answer given 8
0 votes
0 votes
1 answer
3
Mr khan 3 asked Nov 3, 2018
1,040 views
#include<stdio.h void fun(int *p,int *q) { p=q; *p=q; } int i=0,j=1; int main() { fun(&i,&j); printf("%d%d",i,j); }What will be the output of i and j in 16-bit C Compiler...