Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pointer position reset

Tags:

c

pointers

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.

like image 660
kiran Avatar asked Jun 03 '11 06:06

kiran


People also ask

How do you reset a file pointer?

When you open an os. File and read some or all its contents, the file pointer offset will be set to the last line that was read. If you want to reset the pointer to the beginning of the file, you need to call the Seek() method on the file. Thanks for reading, and happy coding!

Which function resets file pointer to the beginning of a file?

fseek(fptr, 0, SEEK_SET); to reset the pointer to the start of the file.


2 Answers

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;
like image 186
sharptooth Avatar answered Nov 15 '22 23:11

sharptooth


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.

like image 44
Mike N. Avatar answered Nov 16 '22 01:11

Mike N.