I'm working on a program that modifies the data of an array using only pointers. I'm trying to return the index where array[ix]==0. However, I keep getting stuck in an infinite loop. What am I doing wrong?
int firstprime(int size, int arr[]){
int* end = arr + size;
int* begin = arr;
while(begin<end){
if(*begin==0)
return *begin;
begin++;
}
return -1;
}
*beginis exactly equal tobegin[0]. The dereference operator (unary*) gives you the value where the pointer is pointing, nothing else.0, and if you use that returned value as part of a loop condition it will lead to problems, including possible infinite loops.