kiran
kiran

Reputation: 177

pointer position reset

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

Answers (2)

Mike N.
Mike N.

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

sharptooth
sharptooth

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( &current );
}
current = original;

Upvotes: 15

Related Questions