C | Advanced Pointer | Question 3

filter_none

edit
close

play_arrow

link
brightness_4
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;
}

chevron_right


(A) 2 5
(B) Garbage Value
(C) Compiler Error
(D) Segmentation Fault


Answer: (A)

Explanation: The program prints “2 5″.

Since compilers convert array operations in pointers before accessing the array elements, (a+1) points to 2.

The expression (&a + 1) is actually an address just after end of array ( after address of 5 ) because &a contains address of an item of size 5*integer_size and when we do (&a + 1) the pointer is incremented by 5*integer_size.

ptr is type-casted to int * so when we do ptr -1, we get address of 5

Quiz of this Question



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.