1,566 views

2 Answers

Best answer
4 votes
4 votes

Suppose here address of a=1000

Now See &a+1=1000+30*20*10*4=25000

See Next statement

 printf("%ld",&a+1 - &a);

Here subtraction  is

Final Result  = ((a+1) - (a)) / Size of (a)

So here Output=25000-1000/24000

                         =1

Alternatively:

&a+1 - &a

We need to do this subtraction only. Thing is when we use '&' we get a pointer and so everything else becomes pointer arithmetic and not integer arithmetic. But pointer arithmetic is so simple if one applies logic (less rules and cases). 

&a + 1 - adds sizeof(a) to address of a (true for any datatype a)

x - &a - finds the no. of elements of type a that can come between the addresses x and &a or is equivalently ((int)x - (int)(&a))/sizeof(a) 

So, totally we get in terms of integer arithmetic
(&a + sizeof(a) - &a)/sizeof(a) = 1. 

selected by
1 votes
1 votes

&a means starting address of 3D array. Now 3D arrary will contain its element as 2D array. 

So &a+1 will add size of one 2D array. So 2D array is of size 20*30=600 elements

Now lets say size of int in a machine is 4 Bytes amd Base address of 3D array is 0.

So, &a+1 will give 0+(600*4)=2400

Now &a is 0

so (&a+1)-&a will  give no of elements in bw these two addresses.

So, (2400-0)/2400 =1. 

Here i have divided by 2400 becz one element of 3D array is a 2D array and size of 2D array is 2400.

It is same as 1D array will have element of size of data type.

and 2D array will have elments of size 1D array.

Related questions

0 votes
0 votes
0 answers
1
0 votes
0 votes
1 answer
2
Desert_Warrior asked May 16, 2016
604 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...
1 votes
1 votes
1 answer
3
0 votes
0 votes
1 answer
4