@SuricrasiaOnline I'm curious what "array[i] == i[array]" is referring to specifically but search engines are not equipped for that kind of query.

@oakreef @SuricrasiaOnline

in C, array[i] and i[array] are equivalent. In the end of the day, it's just *(array+i).

Though I'm not sure how this is affected by element size...

@wolf480pl @oakreef @SuricrasiaOnline adding 1 to a pointer increments it to point to the next item. So on x86 (or most risc) given `u32 *p = ...; u32 *q =p +1;`, p and q will differ by 4 if you cast them to intptr_t.

(There are some obscure ISAs where this isn't true, but few people try to run off-the-shelf C code on those, these days. The ones I'm aware of are AS/400, Cray UNICOS, and the TI DSP families. Even on those, when they have a C compiler, the equivalency a[i] == i[a] still holds.)


@eqe @oakreef @SuricrasiaOnline hmm I guess if one of the things being added is an integer, there's no doubt whose element size should be taken. It's not like you're doing array1[array2]

@wolf480pl @oakreef @SuricrasiaOnline yep, exactly! Lots of things in C turn into integers automatically, but not pointers.

A partial list of things you can index into a C array with:

Characters, tbl['a']
booleans, tbl[x==3]
floating point I think? tbl[sin(x)]

And strings are also treated as pointers, so you can do
c = "abcde"[4]
Combining that with the original curséd syntax we have
c = 4["abcde"]

Sign in to participate in the conversation

The social network of the future: No ads, no corporate surveillance, ethical design, and decentralization! Own your data with Mastodon!