# 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

7/12/2020 8:45:16 PM

Nunwa_Tezpur10 Answers

New AnswerNunwa_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...

Look at it like this "a" stores integers and each cell has address which is not same as value stored at that address. For p, the addresses (pointers) themselves are values, stored in cells which themselves have an address and are not same as values stored. 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 We also have int **ptr = p, initially both pointing to same address. Then ptr is incremented, ptr is now pointing to cell 1, while p is still at cell 0. Subtracting the two we get no of elements between them which is 1. Then we print **ptr. The first dereference give you the value stored at cell 1 which is the address of a+3, the second dereference gives you the value stored at address (a+3) which is 40

**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.

When you subtract pointers pointing to linear block of memory, then subtracting two pointers from that block will give you number of elements between those two memory addresses. int** ptr = p; // ptr was made to the same address where p was pointing (initially at the start of the array (0th element) Then pointer is incremented by 1, it will now point to 1st element (0 based address) now ptr-p will give no of no elements between them which is 1. Try this printf("%p", (void*)p); printf("%p", (void*)ptr); // both will print the same value. now print printf("%p", (void*)(ptr+1)); notedown the value printed and subtract ptr value you got in the first case, you should see 1 ptr +1 - p = ptr + 1 - ptr = 1

The number of elements wrt to p since you are subtracting cell addresses of p. You cannot find which element(s) directly. The whole thing is like this // array storing values a = 10 20 30 40 50 // addresses of cell a and values stored &a[0] = 1 (not showing dereferencing) &a[1] = 2, &a[2] = 3, &a[3] = 4, &a[4] = 5 // values stored in p p = &a[0], &a[3], &a[4], &a[1], &a[2] // addresses of cell p and values stored &p[0] = &a[0] (not showing dereferencing) &p[1] = &a[3], &p[2] = &a[4], &p[3] = &a[1], &p[4] = &a[2] &p[1] - &p[0] = 1 element ptr after increment = p[1] *ptr = &a[3] **ptr = *(&a[3]) = 40

No you cannot find directly. Although it is easier to figure it out in 1D array or 1D array simulated using pointer by knowing the lhs and rhs addresses (no of elements = rhs - lhs). But in multiple level of indirection you will need to trace it all the way back to find the exact elements since you get no of elements wrt to current pointer/N dimension arrays

thanks a lot .u made it crystal btw is there any way to find that particular element

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

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 ~

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 ~