Archived

This topic is now archived and is closed to further replies.

tj963

Position in an array

Recommended Posts

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

Share this post


Link to post
Share on other sites
yes actually.......
say you have this
  
int array[500];
int* pointer = points to whatever element you want;

// here is the meat

int 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

Share this post


Link to post
Share on other sites
yep. if an array is a relativly continuous segment of memory, then you should be able to get an 'index' by:
  
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

Share this post


Link to post
Share on other sites