Array with negative subscript

17, May, 2018

Have you know that

a[-1]

is valid C? Of course, only if

*(a-1)

exists. So you may do something like:

int _a[N];
int *a = &(_a[N/2]);

for (int i = -N/2; i < N/2; ++i) {
    a[i] = ...
}
But, why? One application I have encounterd is sprite engine for retro machine. It needs to render and handle collisions. Rendering is done by simple blitting memory. For collision testing I used AABB. It is represented by point (in center) and half size vector. This notation produces simpler and faster code than pattern with min/max points. But, now you need to shift your image coordinates by half of the extent in negative direction. No, more. With above pattern blitting with the same coordinates is possible.