C Programming Reference
 
C Programming Reference >> C  Programming Reference Question Bank >>

19/07/07 - 453 Views - No Ratings Yet

Level - Intermediate
      

Question -

What will be  the output of the following code?
#include <stdio.h>

int main()  
{    
  int  a[5] = {1,2,3,4,5};    
  int *ptr =  (int*)(&a+1);      
 printf("%d %d" , *(a+1), *(ptr-1) );   
 return 0;
 }  
Answer -
 2 5
Explanation -

Here 'a' is an array of 5 elements. Now reason for 2 is very simple since *(a+1) is equivalent to a[1].

Now a little difficult part is how the second value displayed is 5. The reason for this (&a + 1) returns the address of next memmory location just after array. It should not be confused with &(a+1) which will return address of a[1]. So now when we display *(ptr - 1) value of last element of array a that is 5 is displayed.

Reader Comments -

Author Comments
Add Comments 


Name :    
Reply :   


Rating :

Code :
Code

 

 
© 2006 cencyclopedia.com