title: Why does this code returns segmentation fault when I try to use binary search to count the number of occurrences of a number in an array recursively?
body: <pre><code>  int countOccurences(int *array, int start, int end, int n)
     {
      int count=0,mid;
      mid=(start+end)/2;
      if(start==end &amp;&amp; array[mid]!=n)
          return count;
      if(array[mid]==n)
          return count+countOccurences(array,mid+1,end,n)+countOccurences(array,start,mid-1,n);
      else if(array[mid]&gt;n)
          return countOccurences(array,start,mid-1,n);
      else if(array[mid]&lt;n)
          return countOccurences(array,mid+1,end,n);
      return count;
  }
  </code></pre>
  
  <p>I'm getting correct results for the item that does not belong to the list but my code returns segmentation fault for the items that exists in my array. Where am I going wrong?</p>