Reputation: 177
I have a pointer pointed to an array and is incremented every time a data is read. Each data is of different length and so I use strlen to jump the pointer. How do I reset the pointer back to its starting address?! Thank you for your help.
Upvotes: 11
Views: 24916
Reputation: 358
I think your best bet would be to simply make a copy of the pointer, then whenever you need to reference the first element you just use the new copy. Example:
int *array = ..;
int *beginning = array;
If you need to reference the first element, or even copy the starting address to the original pointer, you just use the beginning pointer.
Upvotes: 5
Reputation: 170509
Store the original value in another pointer, then assign that stored value back.
char* original;
char* current;
current = wherePointerShouldPointAtStart();
original = current;
while( someCondition() ) {
usePointer( ¤t );
}
current = original;
Upvotes: 15