C Programming - Pointers - Discussion
Discussion Forum : Pointers - Find Output of Program (Q.No. 13)
                   
                                       
                                13.
What will be the output of the program?
 
                                    #include<stdio.h>
int main()
{
    int arr[3] = {2, 3, 4};
    char *p;
    p = arr;
    p = (char*)((int*)(p));
    printf("%d, ", *p);
    p = (int*)(p+1);
    printf("%d", *p);
    return 0;
}
Discussion:
87 comments Page 9 of 9.
                
                        Sai said: 
                         
                        1 decade ago
                
                Please explain about
p = (int*)(p+1);
printf("%d", *p);
                p = (int*)(p+1);
printf("%d", *p);
                        Prateek said: 
                         
                        1 decade ago
                
                p is a char. pointer and  it  is assigned to  complete address  of arr array. so  when  we write  (ar+1)  it  take address  next  beyond  arr  array.  which  having  value  0  of  defined  pointer  array.
                
                        Sundar said: 
                         
                        2 decades ago
                
                The char pointer only stores the first byte address of integer. So while increment the pointer the next value is to be printed as 0.
                
                        Rajadurai said: 
                         
                        2 decades ago
                
                The answer is correct. We can convert int  pointer to char pointer and viz versa. *p is apoint to first variable. *p+1 points the next array, so the answer is zero.
                
                        DRukus said: 
                         
                        2 decades ago
                
                p is declared as a char pointer but arr is an integer array
the assignment p = arr produces a compile-time error.
                the assignment p = arr produces a compile-time error.
                        Ravi said: 
                         
                        2 decades ago
                
                p=arr;//here p is a character pointer. we should not assign int array base address...
                
                        Badareenath said: 
                         
                        2 decades ago
                
                Can anybody explain this please ?
                Post your comments here:
 
            
        Quick links
                            Quantitative Aptitude
                                    
                                    Verbal (English)
                                    
                                    Reasoning
                                    
                                Programming
                                    
                                    Interview
                                    
                                     Placement Papers