What data type would I use to store the address of other elements in an array.
As in, element 0 of an array has the address of element 5. And so on..
What data type would I use to store the address of other elements in an array.
As in, element 0 of an array has the address of element 5. And so on..
A pointer can be converted into an int
(or a long
if you are doing 64-bit and your c compiler defines long
as 64 bits). Then you need only get the address of the element you are trying to point to.
int test[10];
test[0] = (int)&test[5];
The address of an element has a different data type to the element. So your code is going to have to involve some type conversion.
To do it with minimal casting:
T some_object;
void *array[20];
array[0] = &some_object;
array[5] = &array[0];
// ....
T *some_ptr = *(void **)array[5];