3 ms·
This “trick” has been know at least as far back as 2008: https://stackoverflow.com/questions/381542/with-arrays-why-is-it-the-case-that-a5-5a https://stackoverf
by colejohnson66 5y ago
This “trick” has been know at least as far back as 2008: https://stackoverflow.com/questions/381542/with-arrays-why-is-it-the-case-that-a5-5a https://stackoverflow.com/questions/381542/with-arrays-why-i...
- lifthrasiir 5y agoOne of the very first IOCCC winners used the trick in 1984 (1984/anonymous): int i;main(){for(;i["]<i;++i){--i;}"];read('-'-'-',i+++"hell\ o, world!\n",'/'/'/'));}read(j,i,p){write(j/p+p,i---j,i/i);}
- jakeva 5y agowhat does this program do? i compiled it and ran it and got no output. i hope i haven't fork bombed myself or something
- lifthrasiir 5y agoPrints "hello, world!", assuming sizeof(int) == sizeof(char*) (and the same alignments and ABI, etc).
- jfrunyon 5y agoI think this is equivalent to: int i; int main(){ for( ; i["]<i;++i){--i;}"]; // loop until i == 14? (the NUL byte on the end) read('-'-'-',i+++"hello, world!\n",'/'/'/') // read(0, <one byte of "hello, world!\n" at a time>, 1) ) {}; } int read(j,i,p){ write(j/p+p,i---j,i/i); // write(0/1+1, i-- - 0, 1) --> write(1, i, 1) --> write a byte to STDOUT }
- tialaramex 5y agoIt's actually spelled out in K&R in more technical language: "The array subscripting operation is defined so that E1[E2] is identical to *(E1+E2). Therefore, despite its asymmetrical appearance, subscripting is a commutative operation." (my emphasis)