edited by
1,216 views
3 votes
3 votes
#include <stdio.h>
int main() {
     int A[3][4] = {{1,2,3,4},
	            {4,5,6,7},
	            {8,9,10,11}
                   };
     int *p = &A[0][0];
     int *q = (int*)(&A[0]+1);
     int *z = (int*)(&A+1);
     int *w = (int*)A;
     printf("%d\n",*(p + 1));
     printf("%d\n",*(q - 2));
     printf("%d\n",*(z - 2));
     printf("%u %d\n",w,*(w+1));

}
edited by

2 Answers

3 votes
3 votes

&a is of type int (*)[10] (which acts like a pointer to an array)
a is of type int [10] (which acts like a pointer to a single element)

So when you add 1 keep those types in mind. The pointer will be offset by the size of the type that the address contains. a+1 offsets by the size of int, i.e. to the second element in the array. &a+1 offsets completely past the whole array. 

Here address of A=1000(assume) and size of integer=2B.

i) int *p=&A[0][0]; 

p is pointer it stores the address of A[0][0].it is nothing but starting element address which is equall to A.

ii) int *q=(int*)(&A[0]+1);

q is a poinetr it stores address of A[1].

A[0] it means select the 1st dimension array +1 means skip 1 dimension=A[1].

iii) int *z=(int*)(&A+1)

it skips entire Array.

z stores outside value,1024( Array address 1000-1022)

iv) int *w=(int*) A. it stores Address of array A.

printf:*(p+1)=skip one element in forward direction. so it access A[0][1]=2.

printf:*(q-2)=skip two elemnts in backward direction.so it access A[0][2]=3

printf:*(z-2)=skip two elements in backward direction.so it access A[2][2]=10

printf:w=it is nothing but address of A,varies from different compilers(Garbage value)

but we consider in this problem Address of A=1000,so w=1000.

printf:*(w+1)=same as *(p+1)=2.

so final printed values are 2,3,10,1000,2

edited by
1 votes
1 votes

It is a 2D array.Say starting address is 200.

int *p=&A[0][0];//200

Here p pointing to address of the 1st element of 2-D array. So, p takes 200

int *q=(int *)(&A[0]+1);//216

&A[0]=A

&A[0]+1=A+1

So, q pointing to $4\times 4=16$ bit ahead of 1st element of A. i.e. 200+16=216

int *z=(int*)(&A+1);//216

It is same as q, as &A[0]=&A

int *w=(int*)A;//200

Same as p.

Now,

printf("%d\n",*(p+1));//4

p is an 1D array. p+1 is $4\times 4=16B$ ahead.i.e. 216.

printf("%d\n",*(q-2));//1 
printf("%d\n",*(z-2));//1
printf("%u%d\n",w,*(w+1));//400,4

w is 1st address of the array, *(w+1) is 16 bit ahead of w i.e. 416

Related questions

1 votes
1 votes
1 answer
1
srestha asked Jul 27, 2017
416 views
Analyze what is happening and why?int *ptr=(int*)0X12341230; int *ptr2=((int*)(((char*)ptr)+1));char *ptr=(char*) 0X12341230; void *ptr2=ptr+1;
0 votes
0 votes
0 answers
2
garvit_vijai asked Sep 2, 2018
709 views
Please explain the output for the following program: #include<stdio.h>int main() { int i = 100; int *a = &i; float *f = (float *)a; (*f)++; pri...
1 votes
1 votes
1 answer
3
Jaspreet Kaur Bains asked Jan 27, 2018
361 views
what is the meaning of k= (char *) (i*j)it will return k =(char *) (300)
0 votes
0 votes
1 answer
4
Sourabh Kumar asked May 13, 2016
970 views