Home > Software engineering >  Difference of Two Pointers
Difference of Two Pointers

Time:03-28

I'm reading Computer Systems: A Programmer's Perspective. In section 3.8.2 there is an example that says &E[i] - E equals i, assuming E is an array of 4-byte integers. Why isn't the answer 4i?

CodePudding user response:

This is just how pointer arithmetic works in C.

Subtracting pointers gives a result in units of elements, not in units of bytes. It's symmetric with the fact that if you want to access element 2 of the array E, you use E[2] or *(E 2), not E[8] nor *(E 8).

  • Related