POINTER ARITHMETIC

Pointer arithmetic is limited to specific operations:

  1. Addition: Adding an integer to a pointer results in a pointer (ptr + int -> ptr).

  2. Subtraction (Integer): Subtracting an integer from a pointer results in a pointer (ptr - int -> ptr).

  3. Subtraction (Pointer): Subtracting one pointer from another results in an integer (ptr - ptr -> int).

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