I know that the C standard promises that a pointer to any object can roundtrip to a void *
and back. In other words,
T obj;
assert(&obj == (T *) (void *) &obj);
Now, I'm wondering about pointer arithmetic. How does it follow / where does it say that
T *arr[2] = {0};
assert(arr[1] == (T *) (((void **)arr)[1]))
works? Is this even guaranteed?
I have a hunch that the answer is "it's not guaranteed" or even "this is UB". The reason is that, as far as I "know", the representation of void *
need not equal the representation of other pointers. For example, if void pointers are bigger in size than other pointers, pointer arithmetic on void **
probably couldn't work. So unless the above is legal, I wonder what should I do in practice if I want to operate on pointer arrays with generic code.