Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.
It's just one of those things where one day you'll be sitting in front of a problem, and it'll suddenly be obvious you need a negative index.

God, I hope not.
 
I'm being somewhat factious. Of course, the more tools and options the better. But most of my real problems are in algorithm development not necessarily in coding and as a somewhat (alright very) sloppy programmer, the less esoteric tools I have to resort to the better.
 
It's been a while, but I once used negative indexes when processing audio samples (or any time-series of samples, actually). The most natural way to express it was as t[-1], t[0], t[1] for some position of t. That's not the only way to do it, but it was the clearest in that case.

I've also used negative indexes to refer to the previous char in a string buffer, with the advancing ptr indexed by -1.
ptr[-1] is the same expression as *(ptr-1).
 
I don't know about your latter example but you get an A+ for the former. I don't foresee needing it anytime in the near future. (My far future doesn't exist) but I'm going to try to use a few of my remaining brain cells to store that example.
 
It's been a while, but I once used negative indexes when processing audio samples (or any time-series of samples, actually). The most natural way to express it was as t[-1], t[0], t[1] for some position of t. That's not the only way to do it, but it was the clearest in that case.

This is common in graphics, video and image processing as well. The usual way to do this was to allocate enough extra rows of pixel padding around the image so that all negative 2D indexes were still within valid memory.
 
Well, my day job involves a lot of imaging processing. Would coding in that manner speed things up?
 
Some what afraid to ask. But is all this an intellectual exercise in an unintended consequence of C's design or can it serve a real purpose?
Yeah, if you want to learn x86 or x64 assembly language, it can be quite useful. ;-)

----------

Pointer arithmetic is an important part of C - used it myself on occasion; but what code might actual require a negative index; or I have missed something in this discussion.
LZ77/LZ78-based algorithms like deflate in zlib (used in PNG and Apples zlib compressed .dmg files and .xar (.pkg PackageMaker) files (Software Updates)) require "negative" indices. Or if you want to apply a filter to a CG-image. Many applications...

----------

You won't know until you benchmark your particular code. Removing branches (x+i < 0, etc.) might help the compiler unroll more loops.
Unroll your loops, and let the compiler recognize, which loops are "over-optimized". ;-)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.