Pointer Arithmetic & Array Traversal

🟡 Intermediate

📖 Definition

Adding an integer k to a pointer ptr increments the raw memory address by k * sizeof(T) bytes. This forms the foundation of raw array subscript indexing (arr[i] is equivalent to *(arr + i)).

💡 Code Example

#include <iostream>

int main() {
    int arr[] = {10, 20, 30, 40};
    int* ptr = arr;

    for (int i = 0; i < 4; ++i) {
        std::cout << "*(ptr + " << i << ") = " << *(ptr + i) << "\n";
    }
    return 0;
}