top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

C: What will be the output for this Pointer to Pointer Scenario where char pointer is assigned to a integer adddress?

+2 votes
1,318 views
void main(){
   int i=320;
   char *ptr=(char *)&i;
   printf("%d",*ptr); 
}

Please provide your explanation also?

posted May 22, 2015 by Mohammed Hussain

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

The result would be 64.

void main(){ int i=320; char *ptr=(char *)&i; printf("%d",*ptr); }
Suppose address of i, i.e &i = 1000 meaning that 320 is stored in the memory location 1000H.

An integer requires 4 bytes, so 320 would be stored in memory in the following manner --

| < 1003 > | |< 1002 > | |< 1001 > | | <1000 > | --> Memory locations
0000 0000 0000 0000 0000 0001 0100 0000 --> binary representation of 320

The character pointer ptr contains the address of "i" , i.e., 1000H.
Also a character pointer can fetch only 1 byte at a time.
So considering the 0th byte --> 0100 0000 gives 64.

Hope this helps.

answer May 25, 2015 by Ankush Surelia
Similar Questions
+3 votes
#include<stdio.h>

int main(){ 
 int s=2,*r=&s,**q=&r,***p=&q;

 printf("%d",p[0][0][0]);
 return 0;
}

Please provide the explanation of the answer also.

...