3 ms·
That's actually a nice trick. I just hope the compiler doesn't complain about it. I'll have to try that sometime. EDIT: just tried it. The following works:
by cgore 13y ago
That's actually a nice trick. I just hope the compiler doesn't complain about it. I'll have to try that sometime.
EDIT: just tried it. The following works:
#include <stdio.h>
int main(void)
{
int array[100];
int *shifted_array = array - 1980;
for (int i = 0; i < 100; i++)
array[i] = i;
for (int j = 1980; j < 2014; j++)
printf("shifted_array[%d] = %d\n", j, shifted_array[j]);
return 0;
}
- dllthomas 13y agoYeah, there's a couple cases where this kind if thing cleans things up; use it with caution though. More amusing, but less useful outside the IOCCC: The C subscripting operator performs an basically syntactic transformation, turning a[i] into (*(a + i)). This means that, counter-intuitively (if you're thinking of [] as a lookup) the construct i[a] works too because plus is commutative.