393 views
0 votes
0 votes
#include <stdio.h>
int main(void)
{
char a[5] = { 1, 2, 3, 4, 5 };
char *ptr = (char*)(&a + 1);
printf("%d %d\n", *(a + 1), *(ptr - 1));
return 0;
}

(a) Compile Error (b) 2 1 (c) 2 5 (d) none of the above

1 Answer

Best answer
4 votes
4 votes

Answer will be 3) 2 5

When we write 

int arr[size];

Then arr store the base address of array. If you notice carefully then you will see that arr is a pointer to an array of length size

When we write, 

int *p;
p = arr; 
p = &arr;
p = (arr+0);

These all perform the same purpose that give you the base address of array. But when you write 

p = (&arr + 1)

Then it will jump the whole array and give the next address of last element. Because arr is a pointer to array of length size, hence when you increase it by one it will be increased by size.

In behaviour,

int arr1[5];
int (*arr2)[5];

are both same. You may get picture from this. 

selected by

Related questions

0 votes
0 votes
2 answers
1
0 votes
0 votes
1 answer
3
Desert_Warrior asked May 16, 2016
509 views
#include<stdio.h int a = 10; int main() { fun(); fun(); return 0; } int fun() { static int a = 1; printf("%d ",a); a++; return 0; }
0 votes
0 votes
1 answer
4
Desert_Warrior asked May 16, 2016
591 views
#include<stdio.h #include<stdlib.h int main() { char s[] = "Opendays2012"; int i = 0; while(*(s++)) i++; printf("%d",i); return 0; }(a) Segmentation Fault (b) Compile Err...