title: binary search pointers dynamic memory allocation recursion
body: <p>I'm doing an implementation of binary search with pointers and dynamic memory allocation. I first sort the list of elements and then I perform the search. The sorting part works perfectly but my search isn't working properly. I know that the algorithm is correct but I have no idea how to go about the syntax.Could someone help me out?? PS This is my first question on stack overflow, BTW. :)</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int binary_search(int x,int start,int end, int *array)
  {
     int q;
     q=(start+end)/2;
     if(x==*(array+q))
     {
        return q;
     }
     else if (x&gt;*(array+q))
     {
        binary_search(x,q+1,end,array);
     }
  
     else if (x&lt;*(array+q))
     {
        binary_search(x,start,q-1,array);
     }
  
     else if(start&gt;=end)
        return -1;
  }
  
  int main()
  {
     int n,*a,tmp,i,j,search,pos;
     printf("Enter n");
     scanf("%d",&amp;n);
     a=malloc(sizeof(int)*n);
  
     for(i=0;i&lt;n;i++)
     {
        scanf("%d",&amp;tmp);
        *(a+i)=tmp;
     }
  
     for(i=0;i&lt;n-1;i++)
     {
        for(j=0;j&lt;n-1;j++)
        {
           if(*(a+j)&gt;*(a+j+1))
           {
              tmp=*(a+j);
              *(a+j)=*(a+j+1);
              *(a+j+1)=tmp;
           }
        }
     }
  
     printf("Sorted elements are\n");
     for(i=0;i&lt;n;i++)
     {
        printf(" %d",*(a+i));
     }
  
     printf("Enter element to search\n");
     scanf("%d",&amp;search);
     pos=binary_search(search,0,n-1,&amp;a);
     printf("\nThe element is located at position %d",pos);
  
     return 0;
  }
  </code></pre>