Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move pointer in arrays with C

Tags:

arrays

c

file

Is there anyway I can move the pointer in the array. So, like you move where the pointer is pointing at in the array. For files, I know that you can use fseek with something like fread to only copy from a specific location.

I'm trying to copy parts of an array into different files. With fwrite, it doesn't give the option to start from a specific location, so I'll need to move the pointer to somewhere in the array, then copy a part of the array into a file.

like image 584
tenderporkk Avatar asked Oct 24 '25 13:10

tenderporkk


1 Answers

sure you can move a pointer. Say we have an array of ints

int arr[50];

set p to point at the first element

int*p = &arr[0];

now move it along by 10

p += 10;

p now points at arr[10];

Addition on a pointer is defined as incrementing the pointer by n times the size of each elements. Subtraction works too. Be aware of going off either end though, C wont look out for you. Ie this

p += 500;

will compile but will lead to undefined behavior if you try to use p;

like image 142
pm100 Avatar answered Oct 26 '25 12:10

pm100



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!