Monday, May 1, 2017

Pointer Arithmatic.

Adding an integer, say i, to a pointer is actually advancing the pointer to i unit of the underlying data type the pointer points to.  For example,

int *p;
p = &j
p = p + 3;  /* p = p + 3*4 as integer is 4 bytes in length */

Likewise, for subtraction, the pointer is deducted by i units of the underlying data type.

When subtracting 2 pointers, the result is the number of units of the underlying data type.  A minus result indicate the first pointer is less than the second pointer in the expression.

No comments: