c - How does `if(!found)` in this function work? -
this function finds first , last occurrence of integer in array. don't understand second if statement if(!found) same saying if(found==0)? how second statement 'find' first occurrence? lets if there 3 4's in array loop finds last occurrence , sets plast , goes 2nd if statement how know find first occurrence , not 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; }
in c, 0 means boolean false, , other non zero values treated boolean true.
so, if (!found) go true path if found = 0.
if(!found)same sayingif(found==0)?
yes is!
Comments
Post a Comment