POINTER ARITHMETIC
Pointer arithmetic is limited to specific operations:
Addition: Adding an integer to a pointer results in a pointer (ptr + int -> ptr).
Subtraction (Integer): Subtracting an integer from a pointer results in a pointer (ptr - int -> ptr).
Subtraction (Pointer): Subtracting one pointer from another results in an integer (ptr - ptr -> int).
Comparison: Comparing two pointers for equality or inequality returns an integer (boolean result) (ptr == ptr -> int) (ptr ≠ ptr -> int).
In pointer arithmetic, the sizeof operator is automatically used to determine how much to increment or decrement a pointer
ptr = ptr + 1;
* this doesn't just add 1 to the address—it actually adds 1 * sizeof(int), moving the
pointer forward by the size of an int.
- ptr + n → Moves forward by n * sizeof(type) bytes.
- ptr - n → Moves backward by n * sizeof(type) bytes.
- This ensures that the pointer correctly points to the next or previous element
in memory.
Last updated