2,342 views

(int *) a means the base address of the array a ,(int *) a+1 means the address of 2nd element in the array. or base address of 2nd row of the array ? please clear this doubt i am facing problems in these type of question or if anyone knows basics of these types problems please explain that one.

1.   1, 1, 1, 1
2, 2, 2, 2
3, 3, 3, 3
2, 2, 2, 2
3, 3, 3, 3
4, 4, 4, 4
1, 1, 1, 1
2, 3, 2, 3
3, 3, 3, 3
2, 3, 2, 3
3, 3, 3, 3
4, 4, 4, 4
2.   1, 1, 1, 1
2, 4, 2, 4
3, 3, 3, 3
2, 4, 2, 4
3, 3, 3, 3
4, 4, 4, 4
3.   1, 1, 1, 1
2, 4, 2, 4
3, 0, 3, 0
4, 2, 4, 2
5, 5, 5, 5
6, 0, 6, 0

### 1 comment

Plz explain for

i = 1,  j=0

i. = 1 , j= 1

i.  = 1,  j= 2

I am not understand plz explain??

## 1 Answer

Best answer

let int *p = &i;

what it means, address of the integer i is stored in the p. ===> value of p = address of i.

∴ if you use p to require value, then it gives the address of the integer i.

ARR is a two dimensional array.

therefore it is a collection of 1-D array's. when you specified as value of ARR then it returns the base address of 1-D array.

for clarity image https://drive.google.com/open?id=1xGGZKaGH582n_aqwWOgyeGssxpYdjuRB

Note that, *(*(p+i)+j) = p[i][j]; and *(*(p+j)+i) = p[j][i];

when i=0

when j=0 ===> the printf function prints 1,1,1,1

when j=1 ===> the printf function prints 2,2,2,2

when j=2 ===> the printf function prints 3,3,3,3

when i=1

when j=0 ===> the printf function prints 2,2,2,2

when j=1 ===> the printf function prints 3,3,3,3

when j=2 ===> the printf function prints 4,4,4,4

### 16 Comments

Shaik Masthan please ellaborate o/p only for when  i = 0 and j=1 means how 2, 2,2,2 only

i cant see image clearly on mobile so......

@Deepanshu

i cant see image clearly on mobile so......

i shared the file in Google drive also....

let i=0, and j=1 then

*(*(p+i)+j) = *( *(2500+0) + j ) = *( *(2500+(0*size of pointer) ) + j ) = *( *(2500) + j ) = *( 100 + j )

= *( 100 + 1 )

= *( 100 + (1*size of integer) ) = *( 100 + (1*4) ) = *( 100 + 4 ) = *( 104 ) = 2

*(*(p+j)+i) = *( *(2500+1) + i ) = *( *(2500+(1*size of pointer) ) + i ) = *( *(2502) + i ) = *( 104 + i )

= *( 104 + 0 )

= *( 104 + (0*size of integer) ) = *( 104 + (0*4) ) = *( 104 ) = *( 104 ) = 2

Shaik Masthan thanku  :)

shaikh bro ,please tell me that line meaning (int*)arr -value returned by arr converted into integer pointer, i understood the things in image but not getting this line

int *) a+1 means the address of 2nd element in the array please tell about this
(int*)arr ---- the address returned by the arr treated as address of integer

(int*)arr+1 --- it is the next integer address
edited

@shaikh brother first i am sorry for replying late

After doing 2-3 problems on these type questions with the solution that you have provided i understood everything These basics helped me in solving others questions too , you have explained in a very clear manner Actually i was confusing in the (int*)a+1 It is address of second element of array because here (int*)a has higher precedence than + so (int*)a is base address of array a[][] and (int*)a+1 is 100+1*sizeof (int*) =104 Means( int*)a+1 is base address of a[0][1] which is second element of a @shaikh bro see the attachment

What I did and tell me if i am going somewhere wrong ->One more question is such type questions can be asked in gate ? Because they are taking the time

CHECK THIS QUESTION, IT WILL VERY HELPFUL https://gateoverflow.in/254844/3d-array

such type questions can be asked in gate ? Because they are taking the time

i hope no... but learning the concept behind the MULTI-DIMENSIONAL array, these type of questions helpful.

one more suggestion ( neither request nor order ) , take different sizes for pointer and integer then it will be very helpful to solve such questions....

(int*)a+1 is 100+1*sizeof (int*) =104

should be

(int*)a+1 is 100+1*sizeof (int) =104

due to that (int*) a ====> results pointer to int ====> it is pointing to integers

let that results pointer to 1-D array, then we have to multiply by sizeof ( 1-D array )

@Shaik Masthan

Thank you. That answer is very helpful.

I have one doubt, why did you assume pointer size to be 2 bytes? and not 4 bytes.

generally to make difference between pointer and size of integer

we have to assume one is 2B and another is 4B

( we can assume both are 2B or 4B but sometimes it may create confusion, for that purpose i distinct them. )

i preferred 2B for pointer and 4 B for integer due to most of the compilers assumed like this

This is not a rule, this is just convention
okay got it. I will try to apply this rule while solving pointers question wherever needed.

Excellent explanation Shaik Masthan. Many of my doubts were cleared with this answer.

you said that :

*(*(p+i)+j) = p[i][j]; and *(*(p+j)+i) = p[j][i];

but if we take i=0,j=1  than p[0][1]=2 is not equal to p[1][0]=4 thus the  answer should be 2,4,2,4 than why the answer is 2,2,2,2 please explain
You mistaken while understanding what is p[1].

Very well explained @ShaikMasthan

If in question like below

static int *p[]={a,a+1,a+2};

then

output like below

1 1 1 1
2 4 2 4
3 garbage 3 garbage
4 2 4 2
5 5 5 5
6 garbage 6 garbage

Please, correct if I am wrong.