pointers-memory · intermediate · ~15 min

Distance between pointers

Pointer subtraction yields an element count, not a byte count.

Challenge

Implement int ptr_distance(const int *a, const int *b) returning how many int elements b is past a (b - a).

Starter code

int ptr_distance(const int *a, const int *b) {
    /* TODO */
    return 0;
}

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.