2,257 views
0 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 4 votes

Option a is right

3 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/

Position:
Show:

Related questions

3 3 votes
2 answers 2 answers
838
838 views
kirmada asked Jun 20, 2024
838 views
#include <stdio.h int main() { // Write C code here int i=10,*p, q, *r; p=&i; *p=15; q=&p; q=20; r=&q; *r=*p+1; printf("%d",i); return 0; }answer the output as integer ...