Position in an array

Started by
2 comments, last by tj963 22 years, 2 months ago
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
tj963
Advertisement
yes actually.......
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:
  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
Hi,
Thanks for the suggestions. Works perfectly.

tj963
tj963

This topic is closed to new replies.

Advertisement