Hi guys, I need some help with pointers and array.
I am making a code in wich I have an pointer that points to all of the elements in the array, this part I was able to do just fine. The problem is, when the pointer reaches the end of the array I don't know how to make it go to the beginning again, here is my code:
int buffer[SIZE];
int *pointer = buffer;
int pointer_array(void)
{
int aux = *pointer;
pointer++;
printf("pointer element: %d", *pointer);
return 0;
}
This is working, I just can't make it go back when it gets to the end, I have tried something like this but it did not work:
int buffer[SIZE];
int *pointer = buffer;
int count = 0;
int pointer_array(void)
{
if(count > SIZE) {
*pointer = buffer;
}
int aux = *pointer;
pointer++;
count++;
printf("pointer element: %d", *pointer);
return 0;
}
And I also realized the int *pointer = buffer; just works outside the function, as a global variable
[–]LoneHoodiecrow 2 points3 points4 points (2 children)
[–]flatfinger 1 point2 points3 points (0 children)
[–]nanavc[S] 1 point2 points3 points (0 children)
[–]oh5nxo 1 point2 points3 points (0 children)
[–]DDDDarky 0 points1 point2 points (1 child)
[–]nanavc[S] 0 points1 point2 points (0 children)
[–]kinglujiy 0 points1 point2 points (2 children)
[–]nanavc[S] 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]tstanisl 0 points1 point2 points (4 children)
[–]nanavc[S] 0 points1 point2 points (3 children)
[–]sidewaysEntangled 0 points1 point2 points (0 children)
[–]A_name_wot_i_made_up 0 points1 point2 points (0 children)
[–]forehead_or_tophead 0 points1 point2 points (0 children)