0

I do not understand why the "C program" is giving the output 1 in line 1?

int main()
{

    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,%d",ptr-p,**ptr); //line 1
    return 0;
}

I ran the code on CodeBlocks:IDE. The output is 1,40. I got why 40 part. But I don't understand is why 1? From my understanding ptr-p should give the difference in their address and that should be 4 as in 4 bytes.

Please kindly give a detailed explanation.

Thank you.

Sander De Dycker
  • 16,053
  • 1
  • 35
  • 40
  • Difference in address is typed, means that the unit is the number of places that exists to store things in between. If you understand why ptr++ "points" to 40, then a simple arithmetic will give you the result : ptr++ is ptr=ptr+1, so after increment ptr+1-ptr = 1! – Jean-Baptiste Yunès Jan 23 '19 at 12:52
  • alternate duplicate : [Pointer subtraction confusion](https://stackoverflow.com/questions/3238482/pointer-subtraction-confusion) – Sander De Dycker Jan 23 '19 at 13:17

2 Answers2

1

No, that's not how pointer subtraction works. When you subtract two pointers (belonging to the same array), you get the difference in the subscript, i.e., the element number.

Quoting C11, chapter 6.5.6, P7

When two pointers are subtracted, both shall point to elements of the same array object, or one past the last element of the array object; the result is the difference of the subscripts of the two array elements. [....]

That said, the pointer subtraction yields a result of type ptrdiff_t and you should use %td to print the result.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • 1
    You know that this is duplicate, so for the sake of the site, please mark it as is. – Jean-Baptiste Yunès Jan 23 '19 at 12:53
  • @Jean-BaptisteYunès I don't think there is a good canonical dupe for this question. The linked ones were quite bad. – Lundin Jan 23 '19 at 12:56
  • Uh wait I had added such a question to the C FAQ myself... [Why does int pointer '++' increment by 4 rather than 1?](https://stackoverflow.com/questions/5610298/why-does-int-pointer-increment-by-4-rather-than-1). – Lundin Jan 23 '19 at 12:57
  • @Lundin : [Pointer subtraction confusion](https://stackoverflow.com/questions/3238482/pointer-subtraction-confusion) isn't too bad either (found it too late) – Sander De Dycker Jan 23 '19 at 13:05
  • @Lundin This is a standard recurrent question answered at least fifty times... – Jean-Baptiste Yunès Jan 24 '19 at 09:10
1

Pointer arithmetic is done on "pointed-at items", not on bytes. If you have int* ptr = array; ptr++; then ptr is increased by sizeof(int) bytes.

In your case, which is some seriously obscure code, pointer arithmetic is done on a int**. The difference between p and p+1 is 1 item (1 int* pointer item). So you get the result 1.

If you want to know the difference in bytes, you could print the addresses:

int **ptr = p;
printf("%p\n", ptr);
ptr++;
printf("%p\n", ptr);
Lundin
  • 195,001
  • 40
  • 254
  • 396