What's the output?

<syntaxhighlight lang="c"> int main() {

       int arr[3] = {2, 3, 4};
       char *p;
       p = (char*)arr;
       printhex(p);
       printf("%p %d ",p, *p);
       p = p+1;
       printhex(p);
       printf("\n%p %d\n",p,*p);
       return 0;


</syntaxhighlight>


(A) 2 3

(B) 2 0

(C) 1 0

(D) Garbage value


Solution

Assume start location of arr s 1000. So, 2 will be stored as follows:

1000: 0 0 0 0 0 0 0 2 (each char is hexa decimal and assume int takes 4 bytes) 1004: 0 0 0 0 0 0 0 3

Now, %d, *p will print the first 4 bytes starting at 1000, that is 2. when p is incremented, it will go to 1001, as p is a char pointer. 1001: 0 0 0 0 0 0 0 0

so, output will be 0.





blog comments powered by Disqus

What's the output?[edit]

<syntaxhighlight lang="c"> int main() {

       int arr[3] = {2, 3, 4};
       char *p;
       p = (char*)arr;
       printhex(p);
       printf("%p %d ",p, *p);
       p = p+1;
       printhex(p);
       printf("\n%p %d\n",p,*p);
       return 0;


</syntaxhighlight>


(A) 2 3

(B) 2 0

(C) 1 0

(D) Garbage value


Solution[edit]

Assume start location of arr s 1000. So, 2 will be stored as follows:

1000: 0 0 0 0 0 0 0 2 (each char is hexa decimal and assume int takes 4 bytes) 1004: 0 0 0 0 0 0 0 3

Now, %d, *p will print the first 4 bytes starting at 1000, that is 2. when p is incremented, it will go to 1001, as p is a char pointer. 1001: 0 0 0 0 0 0 0 0

so, output will be 0.





blog comments powered by Disqus