Learn how to perform arithmetic operations like incrementing and decrementing on pointers to navigate memory.
Pointer arithmetic in C is a special set of mathematical operations that can be performed on pointers. It's different from regular integer arithmetic. When you perform an arithmetic operation on a pointer, the compiler automatically scales the operation according to the size of the data type the pointer points to. This feature is what makes pointers so effective for traversing arrays. The primary operations you can perform are incrementing (`++`), decrementing (`--`), adding an integer, and subtracting an integer. When you increment a pointer (e.g., `p++`), it doesn't just add 1 to the memory address. Instead, it advances the pointer to point to the *next element* of its type in memory. So, if `p` is an `int` pointer (`sizeof(int)` is typically 4 bytes), `p++` will increase the address stored in `p` by 4. If it were a `char` pointer (`sizeof(char)` is 1 byte), it would increase the address by 1. Similarly, adding an integer `n` to a pointer (`p + n`) calculates the address of the nth element after the one `p` currently points to. Subtracting an integer from a pointer works analogously. You can also subtract one pointer from another (if they point to elements of the same array). The result is not the difference in memory addresses but the number of elements that separate them. These operations are fundamental to using pointers to efficiently iterate through arrays without needing an explicit index variable.