Position in an array
Hi,
If I have a pointer to a position in an array, is there any way I can determine what position it''s pointing at?
Thanks,
tj963
yes actually.......
say you have this
that may or may not work.......i just now made it up
"I pity the fool, thug, or soul who tries to take over the world, then goes home crying to his momma."
- Mr. T
say you have this
int array[500];int* pointer = points to whatever element you want;// here is the meatint elementIndex = (pointer - array)/sizeof(int);
that may or may not work.......i just now made it up
"I pity the fool, thug, or soul who tries to take over the world, then goes home crying to his momma."
- Mr. T
yep. if an array is a relativly continuous segment of memory, then you should be able to get an 'index' by:
you dont need to worry about sizeof because pointers are size specific.
edit: added source tags
Edited by - evilcrap on January 29, 2002 9:46:55 PM
int *arrayStart, *arrayElement;int index = (arrayElement - arrayStart);
you dont need to worry about sizeof because pointers are size specific.
edit: added source tags
Edited by - evilcrap on January 29, 2002 9:46:55 PM
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement