0

static int a[]={10,20,30,40,50}; static int *p[]={a,a+3,a+4,a+1,a+2}; int **ptr=p; ptr++; printf("%d\n%d\n",ptr-p,**ptr)

why the ptr-p =1

12th Jul 2020, 8:45 PM
Nunwa_Tezpur
Nunwa_Tezpur - avatar
6 Answers
+ 3
Nunwa_Tezpur **ptr gives you value 40. But ptr returns you pointer address or memory location value.. So here ptr - p is pointer's subtraction happening, not its content or values pointed by those. **ptr - *p =>40 - 10. And ptr is equal to p+1 according to assingnment **ptr=p; So ptr - p = p+1 - p =1. Hope this clears.. Else @swim give you more clarity...
13th Jul 2020, 9:04 AM
Jayakrishna 🇼🇳
+ 2
**ptr = p; then ptr++ is equal to p+1 then ptr-p = p+1-p =1. [ Note that, Here ptr, p, are return memory location values.] **ptr now points to p+1 = a+3 => that is value 40.
12th Jul 2020, 9:18 PM
Jayakrishna 🇼🇳
+ 1
thanks a lot .u made it crystal btw is there any way to find that particular element
13th Jul 2020, 12:02 PM
Nunwa_Tezpur
Nunwa_Tezpur - avatar
0
correct me if i am wrong ptr gives 40 and p gives 10 so no of elements btw ptr-p is 2 i.e 20 and 30
13th Jul 2020, 7:02 AM
Nunwa_Tezpur
Nunwa_Tezpur - avatar
0
pointer subtraction gives the no of elements between the two address location. so ptr gives 40 and p gives 10 so no of elements btw these two are 2 i.e 20 and 30 ~ swim ~
13th Jul 2020, 9:36 AM
Nunwa_Tezpur
Nunwa_Tezpur - avatar
0
So if you subtract cell addresses of p, you will get number of elements between the addresses so in case of p here we get p + 1 - p = 1 p+1--> a+3 and p-->a are u saying if we subtract the cell address of a+3 and a in pointer p it will give no of elements btw these two addresses which is one element and which is that one element ~ swim ~
13th Jul 2020, 10:29 AM
Nunwa_Tezpur
Nunwa_Tezpur - avatar