This function finds the first and last occurrence of an integer in an array. I don't understand what the second if statement does if(!found)
is it the same as saying if(found==0)
? How does the second statement 'find' the first occurrence? Lets say if there are 3 4's in an array the loop finds the last occurrence and sets it to plast
and than it goes into the 2nd if statement how does it know to find the first occurrence and not the 2nd occurrence?
find_occurences(const int a[], size_t n, int x, size_t *pfirst, size_t *plast) {
size_t i;
int found = 0;
for(i=0; i<; i++)
if (a[i] == x)
*plast = i;
if (!found) {
*pfirst = i;
found = 1;
}
}
return found;
}