({{Template:Author|Arjun Suresh|{{arjunweb}} }})
({{Template:Author|Arjun Suresh|{{arjunweb}} }})
Line 29: Line 29:
 
So, the first two elements of the array will be stored in memory as follows:
 
So, the first two elements of the array will be stored in memory as follows:
  
  1000: 0 2 0 0 0 0 0 0 (hexa decimal representation and assuming int takes 4 bytes and assuming least significant byte is stored at highest address(little-endian))
+
  1000: 0 2 0 0 0 0 0 0 (hexa decimal representation and assuming int takes 4 bytes and assuming least significant byte is stored at lowest address(little-endian))
 
  1004: 0 3 0 0 0 0 0 0
 
  1004: 0 3 0 0 0 0 0 0
  
 
Now, %d, *p.
 
Now, %d, *p.
Here, *p is a character which is just a byte. So, *p value here is the byte at address 1000 which is 00 (in hex) and this is passed to printf. In C language, when a char is passed to a function actually it's int value as given by the char code (usually ASCII) is passed. So, here 00 is extended to 0000 and passed to printf. Now, in printf we used %d, and hence the output will be 2.  
+
Here, *p is a character which is just a byte. So, *p value here is the byte at address 1000 which is 02 (in hex) and this is passed to printf. In C language, when a char is passed to a function actually it's int value as given by the char code (usually ASCII) is passed. So, here 02 is extended to 0002 and passed to printf. Now, in printf we used %d, and hence the output will be 2.  
  
  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.
+
  When p is incremented, it will become to 1001, as p is a char pointer (for pointer addition, sizeof datatype pointed to is added and sizeof char is 1). So, now the value of *p is 00 and as explained above printf will print 0 for this.  
 
  1001: 0 0 0 0 0 0 0 0
 
  1001: 0 0 0 0 0 0 0 0
  

Revision as of 12:14, 18 March 2015

What's the output?

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

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

}


</syntaxhighlight>


(A) 2 3

(B) 2 0

(C) 1 0

(D) Garbage value

Solution by Arjun Suresh

Assume start location of arr is 1000. So, the first two elements of the array will be stored in memory as follows:

1000: 0 2 0 0 0 0 0 0 (hexa decimal representation and assuming int takes 4 bytes and assuming least significant byte is stored at lowest address(little-endian))
1004: 0 3 0 0 0 0 0 0

Now, %d, *p. Here, *p is a character which is just a byte. So, *p value here is the byte at address 1000 which is 02 (in hex) and this is passed to printf. In C language, when a char is passed to a function actually it's int value as given by the char code (usually ASCII) is passed. So, here 02 is extended to 0002 and passed to printf. Now, in printf we used %d, and hence the output will be 2.

When p is incremented, it will become to 1001, as p is a char pointer (for pointer addition, sizeof datatype pointed to is added and sizeof char is 1). So, now the value of *p is 00 and as explained above printf will print 0 for this. 
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" name="pointer_1"> int main() {

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

}


</syntaxhighlight>


(A) 2 3

(B) 2 0

(C) 1 0

(D) Garbage value

Solution by Arjun Suresh[edit]

Assume start location of arr is 1000. So, the first two elements of the array will be stored in memory as follows:

1000: 0 2 0 0 0 0 0 0 (hexa decimal representation and assuming int takes 4 bytes and assuming least significant byte is stored at lowest address(little-endian))
1004: 0 3 0 0 0 0 0 0

Now, %d, *p. Here, *p is a character which is just a byte. So, *p value here is the byte at address 1000 which is 02 (in hex) and this is passed to printf. In C language, when a char is passed to a function actually it's int value as given by the char code (usually ASCII) is passed. So, here 02 is extended to 0002 and passed to printf. Now, in printf we used %d, and hence the output will be 2.

When p is incremented, it will become to 1001, as p is a char pointer (for pointer addition, sizeof datatype pointed to is added and sizeof char is 1). So, now the value of *p is 00 and as explained above printf will print 0 for this. 
1001: 0 0 0 0 0 0 0 0

So, output will be 0.





blog comments powered by Disqus