@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 @wolf480pl @SuricrasiaOnline I'm afraid I'm not familiar with C so this is hard to parse for me. Most my coding experience is higher level stuff like C# and Python.

I know that an asterisk indicates that you're referencing a pointer. So given a pointer p that references 32 bit unsigned integer and another pointer q that references an integer one higher than q...

then you've lost me.

@oakreef @eqe @SuricrasiaOnline

ok, let's say you have a function like this:

void foo(uint8_t array[]) {
// ...

`array` here is actually a pointer to a chunk of memory with 8-bit unsigned ints, one after another.

`*array` dereferences array, and gives you the uint8_t that the pointer points to, i.e. 0-th element of the array

so `*array` is the same as `array[0]`


@oakreef @eqe @SuricrasiaOnline

now, you can get a pointer to the 1-th element by just increasing `array` by one:
`ptr_to_1th = array + 1`
and dereference it:

or just: `*(array + 1)`

this is the same as `array[1]`.

Now, because C treats `a[b]` as just a syntactic sugar for `*(a + b)`, `1[array]` works equally well.

Now the surprising part is if you have array whose elements are bigger than 1-byte


@oakreef @eqe @SuricrasiaOnline

btw. `uint8_t array[]`, when used in function arguments, is just syntactic sugar for:
`uint8_t* array`
i.e. a pointer to uint8_t.

Now if you have an array of uint32_t like:
`uint32_t array[]`
`uint32_t* array`

you'd still want `array[1]` to refer to the 1th element and not 3 bytes of 0th element and 1 byte of 1th element.

But C sees it as:
`*(array + 1)`
so here's the trick:


@oakreef @eqe @SuricrasiaOnline

if array is of type uint32_t*,
then adding one to it advances it by sizeof(uint32_t), in this case 4.

so `array+1` is 4 bytes after `array`, and all is well.

Same for `1+array`.
Same for `1[array]`

I hope this helps.


@wolf480pl Thank you very much for taking the time to explain it to me!

· · Web · 0 · 0 · 1
Sign in to participate in the conversation

Personal server of https://abhlach.ie/@oakreef