4 ms·
I don't know whether the standard guarantees you can index into arrays with large unsigned ints (it says "integer type”. I don’t know whether that means it will
by Someone 5y ago
I don't know whether the standard guarantees you can index into arrays with large unsigned ints (it says "integer type”. I don’t know whether that means it will support both without silently converting them, or whether that means an implementation can pick one)
Also, I don’t know whether it (¿still?) is valid C, but I’ve used negative indexes in the past in graphics applications, where it is convenient to have an 'array' with indices that dont start at zero. One-dimensional example:
#define N 100
char a[2 * N + 1];
char * b = a + 100;
...
for(int x = -N; x <= N; ++x) b[x] = 42;
- tpoacher 5y agoomg that looks horrible! don't do that! xD
- avianes 5y agoThere is an example of negative array indexing within standard lib implementations. Functions like isprint(int c), which checks whether c is a printable ASCII character, can take as input numbers between -128 and 127 or between 0 and 255. To achieve this, implementations usually rely on single array indexable from -128 to 255 (a lookup table). Example from the newlib: https://sourceware.org/git/?p=newlib-cygwin.git;a=blob;f=newlib/libc/ctype/isprint.c;h=e34fbe28a2acf9088d5221de30adfefbfa644259;hb=HEAD https://sourceware.org/git/?p=newlib-cygwin.git;a=blob;f=new... And the array came (indirectly) from: https://sourceware.org/git/?p=newlib-cygwin.git;a=blob;f=newlib/libc/ctype/ctype_.c;hb=8fba45f0ccc3b936edfb93671141bf3fe0b0ee5e https://sourceware.org/git/?p=newlib-cygwin.git;a=blob;f=new...