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 saying if(found==0)?

yes is!


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -